【发布时间】:2011-03-08 19:33:29
【问题描述】:
我有一个来自 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;
}
【问题讨论】:
标签: android