我想从 RecyclerView
的数据集中获取项目在 RecyclerView
中的位置。我的数据集中的每个项目都包含一个唯一 ID。我想在 RecyclerView
中找到对应于数据集中某个 id 的项目的适配器位置。
我的数据集类如下所示。
class dataset {
int id;
JSONArray linked;
int type;
....
}
我根据类型在RecyclerView
中添加TextView
或EditText
最终目标:我的RecyclerView
可以有多个独立的EditText
和TextViews
(虽然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/