我有一个ArrayList<HashMap<Contact, Name>>
,我想用它填充ListView。我可以使用哪种类型的适配器?我应该在适配器的from
字段中输入什么?下面的例子:
String[] from = ?
int[] to = new int[] { R.id.contact, R.id.name});
adapter = new KindOfAdapter(this, R.layout.row, from, to)
希望清楚。感谢任何帮助。
最佳答案
1.实现BaseAdapter
我认为您最好的选择是扩展BaseAdapter并实现以下方法:
getCount()
getItem(int)
getItemId(int)
getView(int, View, ViewGroup)
它看起来像这样:
public class MyAdapter extends BaseAdapter() {
private List<Map<Contact, Name>> map;
private Context context;
public MyAdapter(List<Map<Contact, Name>> map>, Context context) {
this.map = map;
this.context = context;
}
public int getCount() {
return map.size(); // or do you want one list item per name?
// if so, just count out all the map entries in each item of the list
}
public int getItemId(int position) {
return position; // doesn't matter too much...
}
public View getView(int position, View convertView, ViewGroup parent) {
// populate the view here...
// use LayoutInflater.from(context).inflate(resource, parent, false) to inflate new views
}
}
2.谨慎使用ViewHolder模式
在实现getView()时,利用此设计模式将节省大量内存:
http://www.screaming-penguin.com/node/7767