在RecyclerViewAdapter
课程中,我正在传递HashMap<String,ArrayList<Custom>>
作为数据,现在我想获得关键&amp;每个位置的价值,但我不知道我应该怎么做!提前谢谢
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()));
}
答案 0 :(得分:0)
你必须使用entry.getvalue();获得价值
String key = entry.getKey(); ArrayList<Products> value = entry.getValue();
答案 1 :(得分:0)
希望这适合你:
holder.header.setText(dataSet.getKey().toArray()[position]);// for key
holder.productDesc.setText(dataSet.get(position)
.get(productPosition).getDesc()) //sample for getting product description
答案 2 :(得分:0)
如果您尝试使用Value
从HashMap
获取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])));
}
答案 3 :(得分:0)
获得价值:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// key
String key = (new ArrayList<>(tab.keySet())).get(position);
// value
String value = (String) tab.get(key);
TextView textView = holder.textRecyclerView;
textView.setText(value);
}