【问题标题】:How to populate ExpandableListView with data from ArrayList?如何使用 ArrayList 中的数据填充 ExpandableListView?
【发布时间】:2015-02-16 13:03:58
【问题描述】:

我设法从数据库中检索数据并将其存储在 ArrayList 中:

ArrayList<Category> selectCategory = select.all().from(Category.class).execute();

    for (Category category: selectCategory) {
        builder.append(category.name).append("\n");
    }

现在我想将该数据添加到 ExpandableListView 的子元素。以下代码是硬编码的,我不希望这样。我希望它从数据库中获取数据。我怎么做?

List<String> class1= new ArrayList<String>();
    class1.add("English");
    class1.add("Maths");
    class1.add("Geo");

【问题讨论】:

  • 您可以使用 class1.add() 以相同的方式执行此操作。什么是建造者?你也应该给出它的定义和实例化。
  • 所以你的问题不是如何填充 ExpandableListView 而是如何从数据库中获取数据?
  • @greenapps 构建器是:StringBuilder builder = new StringBuilder();
  • @Deutro 我设法获取数据,因为我尝试使用 Toast 将其打印在屏幕上并且它有效。我只是不知道如何将它存储在可扩展列表视图的子元素中......

标签: java android arrays expandablelistview activeandroid


【解决方案1】:

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

我使用本教程创建了我自己的 ExpandableListAdapter。 基本上,您需要一个所有父项的列表和一个附加的 Hashmap,其中父项为键,子项为 List。

List<Parent> parentList = new ArrayList<>();
HashMap<Parent, List<Child>> childHashMap = new HashMap<Parent, List<Child>>

【讨论】:

  • 我使用了相同的教程。感谢您的努力,但在本教程中,字符串是硬编码的,当您启动应用程序时它们会打印在屏幕上。它对我来说也很完美。但我想从我的数据库中获取这些字符串,而不是硬编码。我想做类似 class1.add(category.getFromDB) 的事情。对不起,如果我解释不正确。我刚开始学习android...
  • 你知道如何从你的数据库中获取数据吗?我目前正在使用一个 API 来获取我的数据,所以很遗憾我无法帮助你。所以你的第一个问题是从数据库中获取数据,对吧?
猜你喜欢
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2020-10-30
  • 2014-08-30
  • 2023-03-30
  • 2012-08-30
  • 2018-10-13
  • 1970-01-01
相关资源
最近更新 更多