gpt4 book ai didi

android - 如果我使用 HashMap,我应该如何在 BaseAdapter getView() 中迭代

转载 作者:行者123 更新时间:2023-11-29 00:47:57 26 4
gpt4 key购买 nike

我有一个扩展自 BaseAdapter用作自定义的类 Adapter对于我的 ListView .如果我使用 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;
}

最佳答案

你不能。 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 ListView

关于android - 如果我使用 HashMap,我应该如何在 BaseAdapter getView() 中迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5237315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com