Nested HashMap used for Listviews

Put your problem here if it does not fit any of the other categories.

Nested HashMap used for Listviews

Postby lilkobes08 » Sun Nov 07, 2010 8:59 pm

In my application I have 3 tabs that contain ListViews depending on what the selection was in the previous tabs. Basically, the 1st tab will list restaurants, 2nd tab will lists submenus of the selected restaurant, the 3rd menu will lists the items of that selected submenu, and then when an item is selected, it will display some nutritional info about that item. I don't know the best way to organize the restaurants/submenus/item, but the way I was thinking was a nested HashMap like the following.
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. HashMap<String, HashMap<String, HashMap<String, Double[]>>>
Parsed in 0.001 seconds, using GeSHi

Where it would be set up with something like this.

    Restaurants : Submenus : Items : double[calories,fat,carbs,fiber,protein]

    McDonalds : Sandwiches : Cheeseburger [250,9,31,2,12]
            Dbl Cheesburger [440,23,34,2,25]
        FrenchFries Small Fry [stats]
            medium fry [stats]

    Starbuck's Hot Drinks ... ...

and etc.

What I don't know how to do, is efficiently build this map where it is easy to add/remove things in the future. I was looking towards xml, but I could not figure out how to do it. The following example is kind of how I started, but I really did not have any clue what I was doing.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <util:map name="foodmapping">
  3.         <restaurant key="McDonalds">
  4.                 <util:map>
  6.                 </util:map>"
  8.         </restaurant>
  9.         <restaurant key="Starbucks">
  10.                 <value>2
  11.                 </value>
  12.         </restaurant>
  13. </util:map>
Parsed in 0.001 seconds, using GeSHi

If I did it this way, I would also need help writing the Java code to parse through the xml and put it all into the correct place in its map. I don't think that hard coding every single item in Java would be very efficient, so I was hoping I could get some help.

Posts: 2
Joined: Sun Nov 07, 2010 6:52 pm


Re: Nested HashMap used for Listviews

Postby urbantrad » Mon Nov 08, 2010 1:24 pm

First of all, gosh that's an ugly way to do it. You should create a class for each restaurant, submenu and item - each of them containing their relevant info. That way you only need to keep a list of restaurants, which in turn keep their own lists of submenus and so on.
As for initializing, XML is definitely the way to go. Make sure the XML file is well structured, although i don't think this should be much of an issue. There's a bunch of parsers available, just use your favorite search engine to find tutorials on how to use them.
Senior Developer
Senior Developer
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 3 guests