【问题标题】:Best data structure for key-value with index带索引的键值对的最佳数据结构
【发布时间】:2018-04-19 08:15:24
【问题描述】:

我正在使用 RecyclerView,其中每一行始终与两个字符串对象相关联:行标题字符串和数据字符串(单击行时使用)。

目前,我正在使用两个单独的数组来为我的 recyclerview/adapter 设置提供这些数据。但是,我想清理我的方法并使用可以关联​​行标题字符串和数据字符串的单个数据结构。

我最初的想法是(链接)HashMap,但 RecyclerView.Adapter 使用位置索引,所以我认为这不是最好的方法。

有人可以推荐一个数据结构供我在这里使用吗?希望 Java 或 Android 库中有一些我不知道的好东西。

【问题讨论】:

    标签: android data-structures android-recyclerview key-value


    【解决方案1】:

    您可以创建自定义数据结构并将其存储在 ArrayList 中。对于 RecyclerView,任何时间复杂度为 O(1) 的 DataStructure 都是不错的选择。

    你可以这样做

    public class DataModel {
        private final String title;
        private final String dataString;
        public DataModel(final String title, final String dataString){
            this.title = title;
            this.dataString = dataString;
        }
        // Create getter methods
    }
    

    那么就可以在Adapter中使用如下:

    final List<DataModel> dataModelList = new ArrayList<>();
    

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 2013-01-14
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多