0

I am using retrofit to populate my checklist activity according to its equipment. The data is in json file.

I did try this method to populate the data but whenever there is similar category, it will populate twice. For example {Equipment1, checklist1, date1, activity1}, {Equipment1, checklist2, date2, activity2}, it will populate Equipment1 twice with different checklist correspondingly.

The image below is shows the bug where DP-03 should only come out once and those 2 data need to be in one cardview.

Recycler Card View

JSONObject obj = new JSONObject(jresponse);
String count = obj.getString("count");

if(!(count.equals("0")))
{


    ArrayList<SectionDataModel> sectionDataModelArrayList = new ArrayList<>();
    JSONArray dataArray = obj.getJSONArray("all");


    for (int i= 0; i<dataArray.length();i++)
    {
        SectionDataModel sectionDataModel = new SectionDataModel();
        SingleItemDataModel singleItemDataModel = new SingleItemDataModel();
        JSONObject dataobj = dataArray.getJSONObject(i);
        ArrayList<SingleItemDataModel> singleItemDataModelArrayList = new ArrayList<>();

        sectionDataModel.setEqpName(dataobj.getString("Equipment"));




            singleItemDataModel.setChecklist(dataobj.getString("Checklists"));
            singleItemDataModel.setActivity(dataobj.getString("Activity"));
            singleItemDataModel.setStatus(dataobj.getString("Status"));
            singleItemDataModel.setTime(dataobj.getString("Date"));

singleItemDataModelArrayList.add(singleItemDataModel);
        sectionDataModel.setDescription(singleItemDataModelArrayList);
        sectionDataModelArrayList.add(sectionDataModel);

     }

    // Create RecyclerView Adapter
    recyclerViewDataAdapter = new RecyclerViewDataAdapter(getContext(), sectionDataModelArrayList);

    // Create the recyclerview
    RecyclerView my_recycler_view = view.findViewById(R.id.my_recycler_view);
    my_recycler_view.setHasFixedSize(true);
    my_recycler_view.setAdapter(recyclerViewDataAdapter);
    my_recycler_view.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));

}

else
    {
        Toast.makeText(getContext(), "no data", Toast.LENGTH_SHORT).show();

        final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

        //Set Title
        builder.setTitle("There is no data available");
        builder.setMessage("Please reselect again.");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        builder.show();
}
} catch (JSONException e) {
    e.printStackTrace();
}
  • You can use hashMap with Equipment as key and value as List and each time you try to update key,value you first retrieve already existing list using hashmap.get(equipment1) , then add the new checkList to that list and finally update using hashMap.put(equipment1,updatedChecklist) – because_im_batman Oct 07 '19 at 04:20
  • @schrodingers_cat16 could not use hashmap as there is not only checkliast. There are around 5 to 6 more attribute – Reuben Kai Min Oct 07 '19 at 05:30
  • Try wrapping all those 5-6 attributes into a bundled data model first,then use hashMap ? – because_im_batman Oct 07 '19 at 06:57
  • @schrodingers_cat16 can you provide some example on how to populate the hashmap into nested recyclerview – Reuben Kai Min Oct 08 '19 at 02:33

0 Answers0