Vishnu Roshan
Vishnu Roshan

Reputation: 25

How to display ArrayList<Hashmap<String, String>> in recycler view

I am implementing a search result page where similar results are displayed. I have a Array-List of Hash-Maps(with 50 maps where every map has 12 values). I want to display each map in card-views of the result page. I tried using recycler view for this. but couldn't wrap my mind around it. I already gone through all threads in stack overflow but I couldn't find anything.

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

            }

This is the ArrayList I am getting. kindly give me a example of how to set recycler adapter for this Arraylist.

Upvotes: 0

Views: 4131

Answers (1)

R G
R G

Reputation: 471

Below code might help you. Below is the adapter that displays data into 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);

            }
        }

To set adapter to RecyclerView :

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

Upvotes: 6

Related Questions