user3040029
user3040029

Reputation: 71

How to get value of specific key from listview using entry

I facing a problem of getting all the value from specific key of hashmap listview. I have two listview here, the first listview is to display the data that get from the database and the second listview is to get the data that selected from the first listview. And now, I wish to get all the value of (FOODID3, itemID) from the second listview to display via toast, but I can only get the latest value received from first listview currently. I think the problem is because I set the notifyDataSetChanged on adapter. Please help! Thanks!

This is my first listview that get the data from database

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);
         }
    });
}

This is the second listview that receive the data selected from first listview.

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) 
        {

        }
    });
}

Upvotes: 0

Views: 786

Answers (2)

MineConsulting SRL
MineConsulting SRL

Reputation: 2340

i don't understand do you want to append the third food item to the map? If so you should initialize the map again and anyway this is the wrong way to do it, you should also create a Food object and create an HashMap of Food objects instead of put all the key=>values into it.

Upvotes: 0

dev2d
dev2d

Reputation: 4262

your map can contain duplicate values but can not contain duplicate keys

each time you add element with the existing key name, it overwrites the old value with the new one

please go through Oracle Tutorials - Map

Upvotes: 2

Related Questions