【问题标题】:How to display ArrayList<Hashmap<String, String>> in recycler view如何在回收站视图中显示 ArrayList<Hashmap<String, String>>
【发布时间】:2018-08-06 21:43:10
【问题描述】:

我正在实现一个显示类似结果的搜索结果页面。我有一个哈希映射数组列表(有 50 个映射,每个映射有 12 个值)。我想在结果页面的卡片视图中显示每个地图。我尝试为此使用回收站视图。但无法将我的想法包裹起来。 我已经遍历了堆栈溢出中的所有线程,但我找不到任何东西。

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,String>>();

nodes = docc.getElementsByTagName("Table");
            int k = nodes.getLength();

            for (int i = 0; i < k; i++) {
                Element e = (Element) nodes.item(i);
                HashMap<String, String> map = new HashMap<String, String>();
                patid = getValue(e, "iPat_id");
                patname = getValue(e, "cPat_Name");
                patAge = getValue(e, "iAge");
                cMaritalSt = getValue(e, "cMarital_St");
                OpRegNo = getValue(e, "iReg_No");
                Doc_Id = getValue(e, "iDoc_id");
                Dept_Id = getValue(e, "iDept_id");
                DocName = getValue(e, "cDocName");
                PatAddress = getValue(e, "PatAddress");
                mobileno = getValue(e, "cMobile");
                patdob = getValue(e, "dDob");
                iOP_Reg_No = getValue(e, "iOP_Reg_No");

                map.put("iPat_id", patid);
                map.put("cPat_Name", patname);
                map.put("iAge", patAge);
                map.put("cMarital_St", cMaritalSt);
                map.put("iReg_No", OpRegNo);
                map.put("iDoc_id", Doc_Id);
                map.put("iDept_id", Dept_Id);
                map.put("cDocName", DocName);
                map.put("PatAddress", PatAddress);
                map.put("cMobile", mobileno);
                map.put("dDob", patdob);
                map.put("iOP_Reg_No", iOP_Reg_No);
                mylist.add(map);

            }

这是我得到的 ArrayList。请给我一个示例,说明如何为此 Arraylist 设置回收器适配器。

【问题讨论】:

  • 显示您的代码.. 您尝试在 recyclerView 中显示什么
  • 您实际上想做什么?过滤还是只显示数据?
  • 我需要将地图中的 patname, patAge, DocName 显示为回收站视图中的卡片视图
  • 我只想显示数据@RushabhShah
  • 你需要有一个自定义的 recyclerview 适配器并将你的 myList 传递给你的 RecyclerView 适配器并从适配器中的位置按位置从列表中获取地图

标签: android arraylist hashmap android-recyclerview


【解决方案1】:

以下代码可能会对您有所帮助。下面是向RecyclerView 显示数据的适配器。

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
        private ArrayList<HashMap> aryList;
        private Context context;

        public TestAdapter(Context context, ArrayList<HashMap> aryList) {
            this.context = context;
            this.aryList = aryList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_product, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {

            viewHolder.setIsRecyclable(false);
                viewHolder.tv_iPat_id.setText(aryList.get(position).get("iPat_id").toString());

        }

        @Override
        public int getItemCount() {
            return aryList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            public TextView tv_iPat_id;
             public ViewHolder(View view) {
                super(view);

                            tv_iPat_id = (TextView) view.findViewById(R.id.tv_iPat_id);

            }
        }

将适配器设置为RecyclerView

TestAdapter testAdapter = new TestAdapter(getActivity(), testList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
       rvTest.setLayoutManager(mLayoutManager);
        rvTest.setItemAnimator(new DefaultItemAnimator());
        rvTest.setAdapter(testAdapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2017-06-01
    • 2012-06-18
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多