gpt4 book ai didi

java - HashMap 内的 HashMap ?

转载 作者:行者123 更新时间:2023-12-01 21:08:29 28 4
gpt4 key购买 nike

我有一个可扩展的 ListView ,对于每个 child ,我需要有 4 个“额外”或字符串或其他名称来调用它:- 子标题- 描述- 链接1- 链接2

跟着教程,创建 ListView 、不同的 parent 和 child 只用1个字符串是没有问题的。我尝试在 HashMap 内创建 HashMap ,但它不太有效。什么是更好的解决方案?

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

}

最佳答案

只需创建一个类来保存所有这些信息。

public class Child {

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

}

现在您已经有了一个类,您可以创建与 ListView 中的子级一样多的此类实例,并将它们全部添加到数组列表中并将其传递给您的 ListAdapter。例如,

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

您可以向此列表添加任意数量的子对象,然后将此列表传递给您的 ListAdapter。

关于java - HashMap 内的 HashMap ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784596/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com