我将用什么适配器使用的HashMap在ListView [英] What adapter shall I use to use HashMap in a ListView
问题描述
我想用的HashMap
为适配器项目的列表
为的ListView
。我正想 ArrayAdapter&LT使用;>
但我不能,因为它正在与名单,其中;>
只。我将用什么接口?
I want to use HashMap
for a list of items of Adapter
for a ListView
. I was going to use ArrayAdapter<>
but I can't because it is working with List<>
only. What adapter shall I use?
推荐答案
有没有predefined适配器,将呈现一个HashMap。我建议通过延长BaseAdapter创建自己的适配器。
There are no predefined Adapters which will render a HashMap. I suggest creating your own Adapter by extending BaseAdapter.
编辑:这是更多钞票使用HashMap的使用和扩展BaseAdapter,这里是一个(未经测试),例如:
It is posible to use HashMap with and extended BaseAdapter, here's an(untested) example:
public class HashMapAdapter extends BaseAdapter {
private HashMap<String, String> mData = new HashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(HashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
}
这附带以下警告,这些项目的顺序是不能保证是你加入他们的顺序相同。写这个例子让我看清了;不要在一个适配器使用HashMap的:)
This comes with the following caveat, the order of the items is not guaranteed to be the same order you added them. Writing this example has made me realize; Don't use HashMap in an adapter :)
这篇关于我将用什么适配器使用的HashMap在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!