这似乎适用于 ListItem 是一个包含列表项数据的 POJO ......
public class ListItemAdapter extends ArrayAdapter<ListItem>
{
private static final String TAG = "ListItemAdapter";
private Activity mContext;
private int mLayoutResourceId;
private List<ListItem> mItems = null;
static class ViewHolder
{
public TextView text1;
public TextView text2;
public ImageView image;
}
public ListItemAdapter(Activity context, int textViewResourceId, List<ListItem> items)
{
super(context, textViewResourceId, items);
mContext = context;
mLayoutResourceId = textViewResourceId;
mItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = mContext.getLayoutInflater();
v = inflater.inflate(mLayoutResourceId, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) v.findViewById(android.R.id.text1);
viewHolder.text1.setOnClickListener(new ItemClickListener(position));
viewHolder.text2 = (TextView) v.findViewById(android.R.id.text2);
viewHolder.text2.setOnClickListener(new ItemClickListener(position));
viewHolder.image = (ImageView) v.findViewById(android.R.id.icon);
viewHolder.image.setOnClickListener(new ItemClickListener(position));
v.setTag(viewHolder);
}
ListItem item = mItems.get(position);
Log.v(TAG, item.toString());
ViewHolder holder = (ViewHolder) v.getTag();
String text = item.getText1();
holder.text1.setText(text);
text = item.getText2();
holder.text2.setText(text);
Drawable img = item.getImage();
Log.v(TAG, "image : " + img);
holder.image.setImageDrawable(img);
return v;
}
class ItemClickListener implements View.OnClickListener
{
int position;
public ItemClickListener(int pos)
{
this.position = pos;
}
public void onClick(View v)
{
Toast.makeText(mContext, "Item clicked in row - " + position, Toast.LENGTH_SHORT).show();
};
}
}