我想从第一个hashmap获取所选行并将其显示在另一个hashmap中。我成功地从第一个hashmap获取值并将其显示在第二个hashmap中。现在的问题是如何从第一个hashmap中逐个显示值,因为我目前只能在第二个hashmap中显示一个值。我通过添加入口集或notifyDataSetChanged做了很多研究,但仍然无法正常工作。如我错了请纠正我。请帮忙!谢谢!
这是我的代码。 LISTMENU是第一个hashmap,LISTORDER是第二个hashmap。 LISTORDER可以从LISTMENU的clicktener获得值。
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_LONG)
.show();
listOrder(itemValue, itemID);
}
});
}
private void listOrder(String itemValue, String itemID)
{
ArrayList<HashMap<String, String>> LIST3 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
/*for (Map.Entry<String, String> entry : map.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
}*/
LIST3.add(map);
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
/*adapter.setNotifyOnChange(true);
adapter.addAll(map);
adapter.notifyDataSetChanged();*/
}
答案 0 :(得分:1)
我认为问题是因为您每次都在创建一个新的ArrayList实例。将ArrayList LIST3声明为全局变量,然后在onCreate方法初始化它,在listOrder方法中访问它,而不像下面的代码片段那样创建ArrayList的新实例。希望这有效。
ArrayList<HashMap<String, String>> LIST3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LIST3 = new ArrayList<HashMap<String, String>>();
}
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_LONG)
.show();
listOrder(itemValue, itemID);
}
});
}
private void listOrder(String itemValue, String itemID)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
/*for (Map.Entry<String, String> entry : map.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
}*/
LIST3.add(map);
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
/*adapter.setNotifyOnChange(true);
adapter.addAll(map);
adapter.notifyDataSetChanged();*/
}