这样做的常见方式,以便为您的适配器的getView
。你可以这样做:
class ContactAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
public ContactAdapter(Context context, int textViewResourceId, List<String> items) {
super(context, textViewResourceId, items);
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowcontact, null);
holder = new ViewHolder();
holder.lastname = (TextView) convertView.findViewById(R.id.rowTop);
holder.firstname = (TextView) convertView.findViewById(R.id.rowBottom);
holder.iconContact = (ImageView) convertView.findViewById(R.id.iconContact);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String item = getItem(position);
int i = (int) (Math.random() * 5 + 1);
switch (i) {
...
}
if (item != null) {
holder.lastname.setText(item.split("-")[0]);
holder.firstname.setText(item.split("-")[1]);
holder.iconContact.setImageBitmap(bm);
}
return convertView;
}
}
static class ViewHolder {
TextView lastname;
TextView firstname;
ImageView iconContact;
}
}
你可以找到完整的代码示例there。通过持有者示例,您可以设置4个视图并进行更新。
@Sephy他在这里只问了3个问题,甚至你告诉他接受答案。 –
我发现:[点击这里](http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/)它应该帮助你! – kyp
所以你真的必须编写自己的适配器?我希望有一个更简单的方法,但我会尝试并让你知道,如果它工作。谢谢。 – Daniel