java - 从数据列表中查找项目在 RecyclerView 中的位置

标签 java android android-recyclerview

我想从 RecyclerView 的数据集中获取项目在 RecyclerView 中的位置。我的数据集中的每个项目都包含一个唯一 ID。我想在 RecyclerView 中找到对应于数据集中某个 id 的项目的适配器位置。

我的数据集类如下所示。

class dataset {
    int id;
    JSONArray linked;
    int type;
    ....
}

我根据类型在RecyclerView中添加TextViewEditText

最终目标:我的RecyclerView可以有多个独立的EditTextTextViews(虽然TextView会有一个字段,该字段具有链接到的 EditText 的 ID)。当我单击 TextView 时,我将使用 EditText(dataset) 的 ID 从中获取数据。我想从具有这些 ID 的 RecyclerView 中的项目中获取相应的 EditText 文本。如果我可以从数据集中获取适配器位置,我可以以某种方式使用

recyclerview.getChildAt(position);

但是没有办法从数据集中获取适配器位置。

最佳答案

Hashmap<Integer,Integer> map = new Hashmap<>();
ArrayList<dataset> main_list ; // it contains all objects of dataset

for(int i = 0; i < main_list.size(); i++) {
    int id = main_list.get(i).getId(); // id of the model
    map.put(id, i); // i is the position of adapter
}

map 包含带有每个模型 ID 的适配器位置..

关于java - 从数据列表中查找项目在 RecyclerView 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49098730/

相关文章:

android - 在 Android OpenGL ES 中放大纹理?

java - 适配器 onBindViewHolder 的位置未显示在 toast 中

java - 如何找到两个人的最佳答案

java - 通配符捕获/泛型

android - Android 的新遗物 : can new relic track http requests made using loopj (android-async-http)?

java - 找不到 ARToolkit buildToolsVersion

android - 使用 GridLayoutManager 实现时在 RecyclerView 中居中项目

android - 禁用 RecyclerView 项目拖动时的振动

matlab中parpool的Java异常错误

java - 在 Play 框架中使用 Files.createSymbolicLink 获取 FileSystemException "A required privilege is not held by the client"