1

I want to get the selected row from the first hashmap and display it in another hashmap. And I successfully get the value from first hashmap and display it in second hashmap. Now the problem is how can I display the value one by one that selected from first hashmap because I can only display one value in second hashmap currently. And I have done a lot of research by adding entry set or notifyDataSetChanged but still not working. Correct me if I'm wrong. Please help! Thanks!

This is my code. LISTMENU is first hashmap and LISTORDER is second hashmap. LISTORDER can get the value from clicktener of LISTMENU.

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();*/

}
user3040029
  • 71
  • 2
  • 9

1 Answers1

1

I think the problem is because you are creating a new Instances of ArrayList each and every time. Declare the ArrayList LIST3 as a global variable and then in the onCreate method initialize it access it in the listOrder method without creating a new instance of the ArrayList like the below code snippet. Hope this works.

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();*/

}