3

I want to achieve something like :

  17 July,2018
    - item
    - item
  16 July,2018
    - item
    - item
    - item

I have achieved this successfully with the following link :

Divide elements on groups in RecyclerView or Grouping Recyclerview items ,say by date

But now the problem is in pagination if same date is there in next page also then it will create second header of same date.

i.e . if in first page i have data with date 16 July,2018 and in second page also if i have data continued with date 16 July,2018 then two header are created for both 16 July,2018.

 Page -1
 17 July,2018
        - item
        - item
 16 July,2018
        - item
        - item
 Page -2
 16 July,2018
        - item
        - item
        - item
        - item
 14 July,2018
        - item
        - item

I want all item of one date should be under only one header.. i want to merge two page data if same date..

My code is same as above given link.. still i am adding some important code and if any other code required then please let me know..

 private LinkedHashMap<String, List<NotificationData>> groupDataIntoHashMap(List<NotificationData> listOfPojosOfJsonArray) {

        LinkedHashMap<String, List<NotificationData>> groupedHashMap = new LinkedHashMap<>();

        for (NotificationData pojoOfJsonArray : listOfPojosOfJsonArray) {

            String hashMapKey = pojoOfJsonArray.getTicket_date();

            if (groupedHashMap.containsKey(hashMapKey)) {
                // The key is already in the HashMap; add the pojo object
                // against the existing key.
                groupedHashMap.get(hashMapKey).add(pojoOfJsonArray);


            } else {
                // The key is not there in the HashMap; create a new key-value pair
                List<NotificationData>  list = new ArrayList<>();
                list.add(pojoOfJsonArray);
                groupedHashMap.put(hashMapKey, list);

            }
        }

        return groupedHashMap;
    }

Inside service success :

 try {
            JSONObject object = new JSONObject(response);
            if (!Utils.isEmptyString(response)) {
                if (id == reqIdNotificationList) {

                    if (object.getInt(PARAMS.TAG_STATUS) == PARAMS.SUCCESS_STATUS) {
                        String resultArray = object.getString(PARAMS.TAG_RESULT);
                        if (!Utils.isEmptyString(resultArray)) {

                            Type listType = new TypeToken<List<NotificationData>>() {
                            }.getType();
                            ArrayList<NotificationData> tmpNotification = new Gson().fromJson(resultArray, listType);


                            if (tmpNotificationList != null)
                                tmpNotificationList = new ArrayList<>();

                            if (TextUtils.isEmpty(mtktNo)) {

                                tmpNotificationList.clear();
                                tmpNotificationList.addAll(tmpNotification);

                                LinkedHashMap<String, List<NotificationData>> groupedHashMap = groupDataIntoHashMap(tmpNotificationList);


                                for (String date : groupedHashMap.keySet()) {
                                    dateItem = new DateItem();
                                    dateItem.setTicket_date(date);
                                    consolidatedList.add(dateItem);


                                    for (NotificationData pojoOfJsonArray : groupedHashMap.get(date)) {
                                        generalItem = new GeneralItem();
                                        generalItem.setResult(pojoOfJsonArray);
                                        consolidatedList.add(generalItem);

                                    }

                                }
                                adapter = new NotificationListAdapter(this, consolidatedList, this);
                                rvNotification.setAdapter(adapter);

//                                notificationList.addAll(tmpNotificationList);
//                                adapter = new NotificationListAdapter(NotificationActivity.this, notificationList, this);
//                                rvNotification.setAdapter(adapter);

                            } else {

                                int startIndex = tmpNotification.size();
                                tmpNotificationList.addAll(tmpNotification);

                                Log.d("size",tmpNotificationList.size()+"");

                               adapter.notifyItemRangeInserted(startIndex, tmpNotificationList.size() - 1);

                                LinkedHashMap<String, List<NotificationData>> groupedHashMap = groupDataIntoHashMap(tmpNotificationList);

                                for (String date : groupedHashMap.keySet()) {
                                    DateItem dateItem = new DateItem();
                                    dateItem.setTicket_date(date);
                                    consolidatedList.add(dateItem);


                                    for (NotificationData pojoOfJsonArray : groupedHashMap.get(date)) {
                                        GeneralItem generalItem = new GeneralItem();
                                        generalItem.setResult(pojoOfJsonArray);
                                        consolidatedList.add(generalItem);
                                    }
                                }

                                if (consolidatedList.size() >= object.getInt(PARAMS.TAG_RECORD)) {
                                    rvNotification.removeMoreListener();
                                }


                            }
                            if (tmpNotificationList.isEmpty()) hideMenu();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

Note : In given link it is hashmap but i have used linked hashmap to maintain item order.

Also i have used this linear layout manager.

Thank you !

Annie
  • 2,397
  • 3
  • 18
  • 26
  • You can sort the data according to the date and month and then set it against one header in this way you won't have multiple headers. – Pramod Yadav Jul 23 '18 at 05:17
  • get result orderby on date – Pavya Jul 23 '18 at 05:20
  • There is no issue of order it is already sorted.. but in pagination some data is continued with the same date.. so the headers are created twice.. as new data is added in the list with the same date – Annie Jul 23 '18 at 05:23
  • Its because LinkedHashMap> groupedHashMap = new LinkedHashMap<>(); you are creating new instance every time – Pavya Jul 23 '18 at 05:32
  • @Pavya Yes i have written if conditions for that also that is it's empty or null then create new instance only... but in that case i am getting first page data twice.. 17,16,17,16,14... in that second 16 is perfect with both page data but that duplications.. also due to duplications it scrolls up – Annie Jul 23 '18 at 05:39
  • add your class.. – Pavya Jul 23 '18 at 05:41
  • @Pavya I have added a code of success of API. i am doing a pagination with the id of last item.. also i am using super recyclerview so there is no code for scroll of recyclerview for pagination.. but i can replace that if you have any other solution.. also here mKtno is the id of last item.. – Annie Jul 23 '18 at 05:48
  • @Pavya Can you please help me with this.. i would really appreciate your efforts – Annie Jul 23 '18 at 06:54
  • @Pavya Hey are you there? can you help me in my one of question https://stackoverflow.com/q/52675652/9514919 – Annie Oct 06 '18 at 04:41
  • Did you solve this problem ? @Annie – Mert Gunay Dec 12 '20 at 14:34

0 Answers0