Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort RecyclerView item in android

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

like image 227
Tanvir Durlove Avatar asked Aug 21 '17 06:08

Tanvir Durlove


People also ask

How to integrate sorted recycler view in Android Studio?

Using recycler view we can show grids and list of items. This example demonstrates how to integrate Sorted Recycler View by creating a beautiful student records app that displays student name with age. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Can recyclerviews sort?

Like other adapterviews, recyclerviews need adapter. It is the adapter that maintains the data source on behalf of recyclerview. Given the large use scenarios of recyclerviews, it makes sense to be able to sort them. Example 1: Kotlin Android – RecyclerView Sort Ascending/Descending

What is recyclerview in Android Studio?

RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages.

How to sort multiple data types in Android Studio?

Open the Android Studio. Now close, already open project. From the Menu bar click on File >New> Import Project. Now Choose a Destination Folder, from where you want to import project. Choose an Android Project. Now Click on “OK“. Done, your done importing the project,now edit it. Android SortedList – Sort Multiple Data Types recyclerView


2 Answers

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;             }         }); 
like image 96
Sanat Chandravanshi Avatar answered Sep 20 '22 17:09

Sanat Chandravanshi


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() 
like image 26
Siddhartha Maji Avatar answered Sep 17 '22 17:09

Siddhartha Maji