0

I am using nested collections of String and Hashmap of two strings into Arraylist and populating some data into listview using baseadapter. I want to fetch both strings from HashMap.

Code is here:

1)

ArrayList<HashMap<String, HashMap<String, String>>> Menu = new ArrayList<HashMap<String, HashMap<String, String>>>();

2)

HashMap<String,HashMap<String,String>> mainMenu = new HashMap<String, HashMap<String,String>>();

3)

 HashMap<String,String> subMenu = new HashMap<String,String>();

                for (int k = 0; k < jsonsubmenu.length(); k++) {
                    // get all values from jsonsubmenu JSONArray..
                    String submenuname = jsonsubmenu.optString(k);
                    String price = jsonpeicemenu.optString(k);

                    subMenu.put(submenuname, price);


                }

                mainMenu.put(mainmenu, subMenu);

                Menu.add(mainMenu);

4)

MenuAdapter menuAdapter = new MenuAdapter(
                        RestaurantDetails.this.getParent(), Menu);

                list.setAdapter(menuAdapter);

Above code pieces shows the meaning full flow of usage of collection. I am using below statements for picking the data from selected positions but these statements are not worth. Please suggest me how to fetch data at seleted position.

Stringn menu = Menu.get(position).get(TAG_MENUNAME);
String cost = Menu.get(position).get(TAG_PRICE);
1
  • 1
    Small comment: in mainMenu.put(mainmenu,subMenu) - what is the String mainmenu, should it not be the label for subMenu. Commented May 31, 2013 at 14:14

1 Answer 1

2

I think you want:

String cost = Menu.get(position).get(TAG_MENUNAME).get(TAG_SUBMENU);
Sign up to request clarification or add additional context in comments.

3 Comments

You'll want to enclose that in a try{...}catch(NullPointerException e){...} as the first map might return null.
Depends on if the programmer controls the input or not. Catching NullPointerException are not the recommended way to go.
What do you get? I just came up with TAG_SUBMENU. You'll need to replace that with the actual submenu name. Btw. I don't think you need to wrap the menus in an ArrayList. Use LinkedHashMap<String, LinkedHashMap<String, Integer>> instead to get predictive iteration order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.