这是鸿洋的一篇文章Android 快速开发系列 打造万能的ListView GridView 适配器
里面用到的item是只有一个TextView的,但是我想把item的布局弄复杂一点,因为我是新手,所以看不太懂博客最后的那段代码
// 设置适配器
mListView.setAdapter(mAdapter = new CommonAdapter<Bean>(
getApplicationContext(), mDatas, R.layout.item_list)
{
@Override
public void convert(ViewHolder helper, Bean item)
{
helper.setText(R.id.tv_title, item.getTitle());
helper.setText(R.id.tv_describe, item.getDesc());
helper.setText(R.id.tv_phone, item.getPhone());
helper.setText(R.id.tv_time, item.getTime());
// helper.getView(R.id.tv_title).setOnClickListener(l)
}
});
他博客里面并没有那几个getTitle,getPhone方法,不知道他是怎么改的,还有他那个item的数据,在他之前给的例子里用的是一维数组,没有用到key、value这种形式,所以我想给我的item布局稍微修改复杂一点也不知道怎么改了,希望大家能帮帮忙,谢谢,下面贴出我本身程序的代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.hanksenye.newgridviewlearn.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
ViewHolder.java
package com.hanksenye.newgridviewlearn;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewHolder
{
private final SparseArray<View> mViews;
private int mPosition;
private View mConvertView;
private ViewHolder(Context context, ViewGroup parent, int layoutId,
int position)
{
this.mPosition = position;
this.mViews = new SparseArray<View>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
// setTag
mConvertView.setTag(this);
}
/**
* 拿到一个ViewHolder对象
*
* @param context
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position)
{
if (convertView == null)
{
return new ViewHolder(context, parent, layoutId, position);
}
return (ViewHolder) convertView.getTag();
}
public View getConvertView()
{
return mConvertView;
}
/**
* 通过控件的Id获取对于的控件,如果没有则加入views
*
* @param viewId
* @return
*/
public <T extends View> T getView(int viewId)
{
View view = mViews.get(viewId);
if (view == null)
{
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
/**
* 为TextView设置字符串
*
* @param viewId
* @param text
* @return
*/
public ViewHolder setText(int viewId, String text)
{
TextView view = getView(viewId);
view.setText(text);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param drawableId
* @return
*/
public ViewHolder setImageResource(int viewId, int drawableId)
{
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param bm
* @return
*/
public ViewHolder setImageBitmap(int viewId, Bitmap bm)
{
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
//
// /**
// * 为ImageView设置图片
// *
// * @param viewId
// * @param drawableId
// * @return
// */
// public ViewHolder setImageByUrl(int viewId, String url)
// {
// ImageLoader.getInstance(3, Type.LIFO).loadImage(url,
// (ImageView) getView(viewId));
// return this;
// }
public int getPosition()
{
return mPosition;
}
}
CommonAdapter.java
package com.hanksenye.newgridviewlearn;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public abstract class CommonAdapter<T> extends BaseAdapter
{
protected LayoutInflater mInflater;
protected Context mContext;
protected List<T> mDatas;
protected final int mItemLayoutId;
public CommonAdapter(Context context, List<T> mDatas, int itemLayoutId)
{
this.mContext = context;
this.mInflater = LayoutInflater.from(mContext);
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
}
@Override
public int getCount()
{
return mDatas.size();
}
@Override
public T getItem(int position)
{
return mDatas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder viewHolder = getViewHolder(position, convertView,
parent);
convert(viewHolder, getItem(position));
return viewHolder.getConvertView();
}
public abstract void convert(ViewHolder helper, T item);
private ViewHolder getViewHolder(int position, View convertView,
ViewGroup parent)
{
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
position);
}
}
MainActivity.java
package com.hanksenye.newgridviewlearn;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<String> title = new ArrayList<String>(Arrays.asList("菜名0", "菜名1", "菜名2", "菜名3", "菜名4", "菜名5", "菜名6", "菜名7", "菜名8", "菜名9"));
private List<String> info = new ArrayList<String>(Arrays.asList("¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28", "¥:28"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(new CommonAdapter<String>(getApplicationContext(),title,R.layout.item) {
@Override
public void convert(ViewHolder helper, String item) {
helper.setText(R.id.title, item);
helper.setText(R.id.info, item);
}
});
}
}
就是上面的MainActivity那2个一维数组,我的想法是把它们弄成key、value的形式的,可是adapter是<T>形式,我用key、value形式我用的是<Map<K,V>>,不知道描述的有没有错误,希望有大神解答,现在程序是可以运行的,可是就是item只有一个数据源,希望能给我举多几个例子让我理解一下,谢谢大家
はい、この T がなぜ使われるか知っていますか?実際のアプリケーションでは、多くの場合、データ ソースはエンティティです。
リーリーもう一度考えてみましょう。
では、なぜこの T が使われるか知っていますか?