how to sort card view in recycler view in android

156
March 28, 2019, at 02:00 AM

I have some api response as below:

[      
        {
            accountType: a,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas1
        }, {
            accountType: b,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas2
        }, {
            accountType: c,
            accountId: 1,
            accountStatus: active,
            isDefault: true,
            accountName: texas4
        }, {
            accountType: a,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas5
        }, {
            accountType: b,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas6
        },
        {
            accountType: a,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas7
        }, {
            accountType: b,
            accountId: 1,
            accountStatus: active,
            isDefault: false,
            accountName: texas9
        }
]

I want the isDefault true account to be shown as first cardview with accountType as c then account type sorting should be done like account type a and its all account list and the account type b and all its account list. over all my card should like this

account type c below default card then account type a all cards then account type b all cards

I always want isDefault card to be on top irrespective of its account type then I want to sort card view based on accountType as a,b,c etc. I m displaying account type then card view below in xml layout How to achieve this on Bindview? any help is appreciated

Answer 1

The RecyclerView will display elements in the exact order that you pass them to your adapter. What you need to do is to rearrange your elements in the order you want them to be in and then pass them to the adapter so that they can be shown. A simple example based on your input

//This is just a data class for our API response
class Account {
    String accountType;
    int accountId;
    boolean accountStatus;
    boolean isDefault;
    String accountName;
}
//Lets say that you have your API response in a list as such
List<Account> accountList = new ArrayList<>();
accountList.add(/*Response from API*/);
//Now we create a sorted list based on your rules
List<Account> sortedAccountList = new ArrayList<>();
//First we need the isDefault account
for (Account account : accountList) {
    if (account.isDefault) {
        sortedAccountList.add(account);
        accountList.remove(account);
        break;
    }
}
//Now we add all 'c' type accounts
for (Account account : accountList) {
    if (account.accountType.equals("c")) {
        sortedAccountList.add(account);
        accountList.remove(account);
    }
}
//Do the same as above for the other account types. You can also apply more rules as per your needs.
READ ALSO
how to join user to a channel created in twilio with android

how to join user to a channel created in twilio with android

I have 2 apps on Android, in my app Patient is where I create my client using a token to create a channel in Twilio, my created channel is the following 1Doc_3Pac that is saved in a string, I can join this channel from Patient app without problem using the channel...

90
WorkManager start Worker twice

WorkManager start Worker twice

I have a set of big tasks which I want to execute in background:

106
How can i stream audio from one device to another device over same wifi network?

How can i stream audio from one device to another device over same wifi network?

I have music player and need to add sync play functionality with other mobile for example if 2 or more users are using my music player and want to play same song on all devices then they just connect through same network and can play music on all devices...

87
Is it possible to get a List of custom objects from Firestore?

Is it possible to get a List of custom objects from Firestore?

I have a Cloud Firestore database for music:

104