-3

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 cardView based on accountType as a,b,c etc. I m displaying account type then cardView below in xml layout How to achieve this on Bindview? any help is appreciated

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
hema
  • 1
  • 4

1 Answers1

-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.
Bilal Naeem
  • 962
  • 7
  • 13