我在列表视图中传递一个HashMap BaseAdapter工作正常。
当我在我的适配器中传递三个HashMap时,它给出了与之前相同的值
如何通过多个HashMap我不知道
这是MyAdapter
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(HashMap<String, Integer> name) {
mData = new ArrayList();
mData.addAll(name.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry<String, Integer> getItem(int position) {
return (Map.Entry) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_order_list, parent, false);
} else {
result = convertView;
}
Map.Entry<String, Integer> item = getItem(position);
String value = String.valueOf(item.getValue());
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(R.id.Name)).setText(item.getKey());
((TextView) result.findViewById(R.id.Quantity)).setText(value);
return result;
}
}
提前感谢您的帮助
答案 0 :(得分:2)
当我在我的适配器中传递三个HashMap时,它给出了与之前相同的值
由于您没有向数据集添加内容的方法,因此您可能会调用new MyAdapter
三次,并将最后一个实例传递给您的ListView
。您可能希望添加一种方法,将新HashMap
的实例添加到mData
,例如
public void addItem(HashMap<String, Integer> item) {
synchronized(mData) {
mData.add(item)
}
notifyDataSetChanged();
}
使用Adapte
r的实例