我有一个来自BaseAdapter的扩展类,它用作我的ListView的自定义Adapter。如果我使用List<>作为数据集,那么使用getView()方法就不会有任何问题,而且一切都很好-我的列表中填充了来自List的所有数据。但是如果我使用HashMap,这永远不会起作用。据我所知,getView()遍历集合,这在List中工作得很好,因为它是可迭代的。
private class MAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.list_item, null);
}
Task task = taskList.get(position); // works perfect if taskList is List and doesn't work if it is HashMap. What shell I do to use HashMap here?
if (task != null) {
TextView tvDescription = (TextView) v
.findViewById(R.id.task_text_view);
TextView tvTime = (TextView) v
.findViewById(R.id.time_text_view);
if (tvDescription != null) {
tvDescription.setText(task.getDescription());
}
if (tvTime != null) {
tvTime.setText(task.showTime());
}
}
return v;
}发布于 2011-03-09 03:36:57
你不能。BaseAdapter实现了ListAdapter,它被称为ListAdapter是有原因的。为什么要使用HashMap?
更新:
从列表中删除。
for(int j = list.size() - 1; j >= 0; j--){
Object o = list.get(j);
if(o.getId().equals(id)) {
list.remove(o); // find obj and remove
break;
}
}
adapter.notifyDataSetChanged(); // update ListView或者同时保留一个列表和HashMap。我建议只使用列表。
Object o = map.remove(id); // remove object by id
list.remove(o); // remvove object from list
adapter.notifyDataSetChanged(); // update ListViewhttps://stackoverflow.com/questions/5237315
复制相似问题