從listview中獲取特定鍵的值我面臨hashmap listview的getting all the value from specific key
問題。我在這裏有兩個listview,第一個listview是顯示從數據庫獲得的數據,第二個listview是從第一個listview中獲取數據。現在,我希望從第二個列表視圖中獲取所有(FOODID3,itemID)的值以通過烤麪包顯示,但我只能獲取當前從第一個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)
{
}
});
}
映射不能包含給定鍵的多個值。 – njzk2