33

I'm working on a android chatting application. When I called my api it returns me the chat list sorted by a user_id. But what I need to do is serialized by message_id as I want to show last message first.Here is my onBindViewHolder method in which i get the values.

public void onBindViewHolder(final MyAdapter_HomeViewHolder holder, final int position) {

    holder.userNameTV.setText(data.get(position).getUserInfo().getFullName());
    holder.msgBodyTV.setText(data.get(position).getBody());
    holder.originator_iD.setText(data.get(position).getUserInfo().getId().toString());

    //message ID. I need to serialize my recyclerView by this ID.biggest id should appear first.
    holder.messageId.setText(data.get(position).getId().toString());

    holder.owner_type_ET.setText("1");
    holder.subject_ET.setText("Message");

}

In case of you need to see the full code, https://pastebin.com/Zxmq36Gn

Ramees Thattarath
  • 1,093
  • 11
  • 19
Tanvir Durlove
  • 768
  • 2
  • 9
  • 21
  • 2
    Do you mean serialize or sort? I'm assuming you're using a recycler view, so what you need to do (assuming you meant sort) is sort the list whenever a new item is added and then calling "notifyDataSetChanged()" on your adapter. That can be done by implementing a comparator function for your objects in the recycler view. [See here](https://stackoverflow.com/questions/5805602/how-to-sort-list-of-objects-by-some-property) for how to sort – swerly Aug 21 '17 at 06:22
  • 1
    Yes I mean shorting by the message_id. Need to show biggest message_id at the top. and notifyDataSetChanged() is returning my user_id as usual. – Tanvir Durlove Aug 21 '17 at 06:25
  • Check that link I posted for how to sort your List. In your constructor you can sort the list before you set it as a the member variable: `public MyAdapter_home(List data, int rowLayout, Context context) { //sort data here this.data = data; this.rowLayout = rowLayout; this.context = context; }` – swerly Aug 21 '17 at 06:28
  • I did it but not worked. Hope you see my full code mentioned above. – Tanvir Durlove Aug 21 '17 at 06:37

8 Answers8

22

try this before passing your list to the adapter (after API call and before adapter notifydatasetchanged):

 Collections.sort(data, new Comparator<CustomData>() {
            @Override
            public int compare(CustomData lhs, CustomData rhs) {
                // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
                return lhs.getId() > rhs.getId() ? -1 : (lhs.customInt < rhs.customInt ) ? 1 : 0;
            }
        });
5

in Kotlin use like this after loading data in array:

myItems.sortWith(Comparator { lhs, rhs ->
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            if (lhs.name > rhs.name) -1 else if (lhs.id < rhs.id) 1 else 0
        })

After that apply:

myItemAdapter.notifyDataSetChanged()
Siddhartha Maji
  • 1,022
  • 8
  • 15
3

A simpler solution for someone still stuck on the same

  Collections.sort(data, new Comparator<CustomData>() {
            @Override
            public int compare(CustomData lhs, CustomData rhs) {
                return Integer.compare( rhs.getId(),lhs.getId());
            }
        });

YourAdapter adapter = new YourAdapter(context, data);

//Setup Linear or Grid Layout manager
 recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 recyclerView.setAdapter(adapter);

Zephania Mwando
  • 118
  • 1
  • 9
2

Before passing the data to RecyclerView adapter

data.sort(new Comparator<Datum>() {
            @Override
            public int compare(Datum o1, Datum o2) {
                return o1.get(position).getMessageId().compareTo(o2.get(position).getMessageId());
            }
        });

then pass (notify) the sorted list to the adapter.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Ramees Thattarath
  • 1,093
  • 11
  • 19
2

Add this lines of code before passing to RecyclerView Adapter

Collections.sort(yourLists, new Comparator<YourList>() {
        @Override
        public int compare(YourList lhs, YourList rhs) {
            return lhs.getId().compareTo(rhs.getId());
        }
    });
Pratik
  • 167
  • 2
  • 10
2
 Collections.sort(response.body(), new Comparator<All_posts>() {
                        @Override
                        public int compare(All_posts lhs, All_posts rhs) {
                            if(lhs.getId() > rhs.getId()) {
                                return -1;
                            } else {
                                return 1;
                            }
                        }
                    });
  • "response.body" is the arraylist I got from json, this is what I pass to the recycler view adapter,

  • "All_posts" is the "Model" class, the one which contains only fields;

  • And getId is the value I want comparison to take place on it, it's from my model class,

I wrote this before I set my adapter to my recycler view. and after I set my adpater to my recyclerView, I wrote recyclerView.getAdapter().notifyDataSetChanged();

Hossein
  • 37
  • 6
0
 List<Items_Detail_model>items_model;
  Collections.sort(items_model, new Comparator<Items_Detail_model>() {
            @Override
            public int compare(Items_Detail_model o1, Items_Detail_model o2) {
                return o1.getDate().compareTo(o2.getDate());
            }
        });
H SOFTZ
  • 21
  • 4
0

Try this before giving list to adapter

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Collections.sort(yourList, Comparator.comparing(YourModel::getFileSize));
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45