android - 关于Adapter类如何从HashMap中获取key和value
问题描述
在我的RecyclerViewAdapter
课堂上,我作为数据传递HashMap<String,ArrayList<Custom>>
,现在我想获取每个位置的键和值,但我不知道我应该怎么做!提前致谢
public class Main_Recycler_Single_Row extends RecyclerView.Adapter<Main_Recycler_Single_Row.ViewHolder> {
private Context mContext;
private HashMap<String,ArrayList<Products>> dataSet;
public Main_Recycler_Single_Row(Context mContext, HashMap<String,ArrayList<Products>> mDataSet) {
this.mContext = mContext;
this.dataSet = mDataSet;
} @Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
Map.Entry<String,ArrayList<Products>> entry=(Map.Entry<String,ArrayList<Products>>) dataSet.entrySet();//this line is my problem!
holder.header.setText(entry.getKey());
holder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
holder.mRecyclerView.setHasFixedSize(true);
holder.mRecyclerView.setAdapter(new Main_Recycler_Sinle_Item(mContext,entry.getValue()));
}
解决方案
如果你想Value
从HashMap
using中获得position
,你还需要Array
维护keys
keys = (String[])mDataSet.keySet().toArray();
您的ViewHolder
课程将如下所示:
public Main_Recycler_Single_Row(Context mContext, HashMap<String,ArrayList<Products>> mDataSet) {
this.mContext = mContext;
this.dataSet = mDataSet;
keys = (String[])mDataSet.keySet().toArray();
} @Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
holder.header.setText(keys[position]);
holder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
holder.mRecyclerView.setHasFixedSize(true);
holder.mRecyclerView.setAdapter(new Main_Recycler_Sinle_Item(mContext,dataSet.get(keys[position])));
}
推荐阅读
- sql - 使用 distinct 和 row_number 分页时的性能
- spring-restdocs - 如何使 Spring Rest Docs 看起来像 Swagger?
- android - ionic 4: android 权限在 prod 构建后不起作用
- postgresql - 匹配邮政编码和城市名称 - 在 PostgreSQL 中非常慢
- xbox - 如何向 Xbox Live Restful API 发送请求?
- arrays - 如何使用 ng-repeat 访问动态数组并获取键值对
- javascript - 为长度创建一个 .row.cells 行
- apache-pig - 通过对连接结果进行说明,我得到了完美的结果,但转储不能正常工作
- tensorflow - 使用 CNN 进行图像回归
- javascript - React:如何在“页面”之间传递数据并处理页面重新加载?