【发布时间】:2013-12-11 15:18:27
【问题描述】:
我遇到了getting all the value from specific key 的 hashmap listview 问题。我这里有两个listview,第一个listview 是显示从数据库中获取的数据,第二个listview 是获取从第一个listview 中选择的数据。现在,我希望从第二个列表视图中获取 (FOODID3, itemID) 的所有值以通过 toast 显示,但我目前只能获取从第一个列表视图收到的最新值。我认为问题是因为我在适配器上设置了 notifyDataSetChanged。请帮忙!谢谢!
这是我第一个从数据库中获取数据的列表视图
private void listMenu(String CID)
{
ArrayList<Object> data = DB.getFood(CID);
LIST2 = new ArrayList<HashMap<String, String>>();
for (int i=0; i<data.size(); i=i+4)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(FOODID2, (String) data.get(i));
map.put(FOODNAME2, (String) data.get(i+1));
map.put(PRICE2, (String) data.get(i+2));
map.put(RATING2, (String) data.get(i+3));
LIST2.add(map);
}
LISTMENU = (ListView) findViewById(R.id.listMenu);
final List2Adapter adapter = new List2Adapter(MainActivity.this, LIST2);
LISTMENU.setAdapter(adapter);
LISTMENU.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
HashMap<String, String> map = LIST2.get(position);
itemValue = map.get(FOODNAME2);
itemID = map.get(FOODID2);
Toast.makeText(getApplicationContext(),
"Food ID : " + itemID + "ListItem : " + itemValue , Toast.LENGTH_SHORT)
.show();
listOrder(itemValue, itemID);
}
});
}
这是接收从第一个列表视图中选择的数据的第二个列表视图。
private void listOrder(String itemValue, String itemID)
{
HashMap<String, String> map = new HashMap<String, String>();
String quantity = "1";
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
map.put(FOODQUANTITY3, quantity);
//problem is here, I can only display the latest data received
for(Entry<String, String> entry: map.entrySet())
{
if(entry.getKey().equals(FOODID3))
{
Toast.makeText(getApplicationContext(),
"EXISTS " + entry.getValue(), Toast.LENGTH_SHORT)
.show();
}
}
LIST3.add(map);
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
adapter.notifyDataSetChanged();
LISTORDER.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
});
}
【问题讨论】:
-
一个映射不能包含给定键的多个值。
标签: java android listview android-listview hashmap