我遇到了hashmap listview getting all the value from specific key
的问题。我这里有两个listview,第一个listview是显示从数据库获取的数据,第二个listview是获取从第一个listview中选择的数据。现在,我希望从第二个列表视图中获取(FOODID3,itemID)的所有值以通过toast显示,但我只能获得当前从第一个listview接收的最新值。我认为问题是因为我在适配器上设置了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)
{
}
});
}
答案 0 :(得分:2)
答案 1 :(得分:0)
我不明白你想把第三个食物附加到地图上吗?如果是这样你应该再次初始化地图,无论如何这是错误的方法,你还应该创建一个Food对象并创建一个Food对象的HashMap,而不是将所有的key =&gt;值放入其中。