这是鸿洋的一篇文章Android 快速开发系列 打造万能的ListView GridView 适配器
里面用到的item是只有一个TextView的,但是我想把item的布局弄复杂一点,因为我是新手,所以看不太懂博客最后的那段代码
// 设置适配器 mListView.setAdapter(mAdapter = new CommonAdapter( 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
item.xml
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 mViews; private int mPosition; private View mConvertView; private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) { this.mPosition = position; this.mViews = new SparseArray(); 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 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 extends BaseAdapter { protected LayoutInflater mInflater; protected Context mContext; protected List mDatas; protected final int mItemLayoutId; public CommonAdapter(Context context, List 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 title = new ArrayList(Arrays.asList("菜名0", "菜名1", "菜名2", "菜名3", "菜名4", "菜名5", "菜名6", "菜名7", "菜名8", "菜名9")); private List info = new ArrayList(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(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是
形式,我用key、value形式我用的是
是,你知道为什么用这个T吗?实际应用中很多时候你的数据源是一个实体。
你再想一下。
那你知道为什么用这个T吗