gpt4 book ai didi

android - 关于Adapter类如何从HashMap获取key和value

转载 作者:行者123 更新时间:2023-11-29 01:01:30 24 4
gpt4 key购买 nike

在我的 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()));

}

最佳答案

如果您尝试使用positionHashMap 获取Value,您还需要维护Array

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])));

}

关于android - 关于Adapter类如何从HashMap获取key和value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com