我有一个哈希映射HashMap <string,arraylist <string =“”>&gt;我想在android中的循环列表中设置这些数组值

时间:2017-06-08 07:10:42

标签: java android hashmap recycler-adapter

我在适配器构造函数中发送了hashmap的值,但这些值未在Recycleadapter中的onBindViewHolder()中设置。

这是HashMap的工作结构。它的值应该反映在RecyclerView#onBindViewHolder

HashMap<String, ArrayList<String>> hashmap = new HashMap<String, ArrayList<String>>();
ArrayList<String> listOne = new ArrayList<String>();
                           listOne.add(presubsFeaturesName);
                            listOne.add(presubsFeaturesUSer);
                            listOne.add(presubsFeaturesClient);
                            listOne.add(presubsFeaturesPassword);


                hashmap.put("A", listOne);
                System.out.println("mBasicDataListdcsdcfrfervw "+mBasicDataList);

                for (HashMap.Entry<String, ArrayList<String>> entry : hashmap.entrySet())
                {
                    String key = entry.getKey();
                    ArrayList<String> values = entry.getValue();
                    System.out.println("Key = " + key);
                    System.out.println("Values = " + values + "n");
                }

        mAdapter = new ComparePlanRecycleAdapter(hashmap);
                mAdapter.notifyDataSetChanged();
                mRecycleview.setHasFixedSize(true);
                //     mAdapter.getItemCount();
                // mAdapter.swap(dataList1);
                FragmentActivity fragmentActivity = getActivity();
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(fragmentActivity);
                mRecycleview.setLayoutManager(mLayoutManager);
                mRecycleview.setItemAnimator(new DefaultItemAnimator());
                //  mRecycleview.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
                mRecycleview.setAdapter(mAdapter);

这是自定义的RecyclerView.Adapter类

class ComparePlanRecycleAdapter extends RecyclerView.Adapter<ComparePlanRecycleAdapter.MyViewHolder> {

  //  private ArrayList<ComparePlan_Record> basicDataList;
 //   private ArrayList<ComparePlan_Record> predataList;
    boolean dataChanged = true;
    HashMap<String, ArrayList<String>> hashmap;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textSubscriptionPlanSettingFeaturesName;
        TextView textBasicsubscriptionPlanSettingValue1;
        TextView textPremiumsubscriptionPlanSettingValue1;

        public MyViewHolder(View view) {
            super(view);
            textSubscriptionPlanSettingFeaturesName = (TextView) view.findViewById(R.id.subsplanname);
            textBasicsubscriptionPlanSettingValue1 = (TextView) view.findViewById(R.id.basicsubsvalue1);
            textPremiumsubscriptionPlanSettingValue1 = (TextView) view.findViewById(R.id.premiumsubsvalue1);
        }
    }
    public ComparePlanRecycleAdapter(HashMap<String, ArrayList<String>> hashmap) {

        this.hashmap = hashmap;
    //    this.predataList = predataList;
        System.out.println("hashmaphashmaphashmap "+hashmap);

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.compareplan_recycleview_listpopulate, parent, false);

        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
     //   final ComparePlan_Record basicRecord = basicDataList.get(position);
     //   final ComparePlan_Record preRecord = basicDataList.get(position);

        System.out.println("hashmaphashma "+hashmap);

      /*  for (int i = 0; i < getItemCount(); i++) {
            holder.textSubscriptionPlanSettingFeaturesName.setText(basicRecord.getSubscriptionPlanSettingFeaturesName());
            holder.textBasicsubscriptionPlanSettingValue1.setText(basicRecord.getBasicsubscriptionPlanSettingValue1());
            holder.textPremiumsubscriptionPlanSettingValue1.setText(basicRecord.getPremiumsubscriptionPlanSettingValue1());
         //   holder.textPremiumsubscriptionPlanSettingValue1.setText("PS");

            System.out.println("basicRecord.getPremiumsubscriptionPlanSettingValue1()"+basicRecord.getPremiumsubscriptionPlanSettingValue1());
        }*/
        }
    @Override
    public int getItemCount() {
        return hashmap.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}

如何在HashMap中设置RecyclerView#onBindViewHolder的值?

1 个答案:

答案 0 :(得分:2)

Hashmap并不保留排序。您应该使用LinkedHashMap来检索Adapter

中的值
LinkedHashMap<String, List<String>> hMap;

onBindViewHolder

List<List<String>> l = new ArrayList<List<String>>(hMap.values());
List yourlist = l.get(position);