0

I have an expandable listview, and for each child I need to have 4 "extras" or strings or whatever to call it: - childtitle - description - link1 - link2

followed by a tutorial, it was no problem to create the listview, the different parents and the child with 1 string only. i have tried to create a hashmap inside a hashmap, but it didn't quite work. what is a better solution?

public void filldata(){

    ArrayList productinfo = new ArrayList<>();
    categories = new HashMap<>();
    topics = new HashMap<>();


    categories.add("FITNESS"); //This would be the parent title
    categories.add("MONEY");

    List<String> Fitness=new ArrayList<>();
    List<String> Money=new ArrayList<>();


    Fitness.add("Product1"); // this would be child1
    List<String> Product1=new ArrayList<>();
    Product1.add("desc1"); //this would be the description of child 1
    Product1.add("link1.1");//this would be the link1 of child 1
    Product1.add("link1.2");//this would be the link2 of child 1


    Fitness.add("Product2"); //child2
    Fitness.add("Product3"); //child3

    Money.add("Product1"); //child 1
    Money.add("Product2"); //   "  2
    Money.add("Product3"); //   "  3

    topics.put(categories.put(productinfo.get(0),Fitness)); //doesn't seem to work like this
    categories.put(productinfo.get(1),Money);

}
Emanuel Graf
  • 756
  • 17
  • 37
  • You are more likely to get help if you include the actual error message text in your questions instead of just saying things like, "doesn't seem to work like this." – Solomon Slow Jan 21 '17 at 21:54

2 Answers2

2

Simply create a class to hold all that information.

public class Child {

String title;
String description;
String link1;
String link2;

}

Now that you have a class you can create as many instances of this class as many children you have in the listview and add them all into an arraylist and pass that to your ListAdapter. For example,

Child firstChild = new Child();
firstChild.title = "First Child";
firstChild.description = "Description 1";
firstChild.link1 = "link1";
firstChild.link2 = "link2";

List<Child> childList = new ArrayList<>();
childList.add(firstChild);

You can add as many Child objects you want to this list and then pass this list to your ListAdapter.

Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44
  • thanks! almost got it right, just extracting the values in the adapter isn't working. I have in the `public View getChildView` method `Child childList = (Child) getChild(groupPosition,childPosition);` and try to acces it with `childList.title` but it tells me layout.Child cannot be cast to java.lang.String – Emanuel Graf Jan 21 '17 at 22:32
  • I'm not really following what you are trying to do there. But check this answer for the adapter side of code [Custom Adapter for List View](http://stackoverflow.com/a/8166802/3857465). – Abhishek Jain Jan 21 '17 at 22:55
1

hashmap inside a hashmap?

Yes.

A HashMap associates keys with values. A key can be any immutable Object that correctly implements the o.equals(Object otherObject) and the o.hashCode() methods, and a value can be any Object at all.

A HashMap is not usually immutable, so you probably don't want to use HashMap objects as keys, but you absolutely can use HashMap objects as values.

topics.put(categories.put(...)); //doesn't seem to work

You didn't say why that doesn't work, but I'm guessing that you couldn't get it to compile. The reason is, you are trying to call topics.put(...) with only one argument---the value returned by categories.put(...). But, the HashMap put(...) method requires two arguments: A key and a value.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57