samedi 17 août 2019

RecyclerView Adapter return limited randomly sorted items

I need to check if the items in the adapter is greater than the limit number, then display the items according to that limit number else return the item count, I do this like below:

public int getItemCount() {
    int ITEMS_LIMIT = 6;
    if(this.articles.size()  >= ITEMS_LIMIT){
        return ITEMS_LIMIT;
    } else {
        return this.articles.size();
    }
}

However, I need the item from the list be randomly displayed: Example if the limit is 6 and the items in the list is 10, then only display 6 items radomly sorted from the list. I tried with the code below, but the app hangs then crashes with IndexOutOfBoundException.

 public int getItemCount() {
        Random random = new Random();
        int ITEMS_LIMIT = 6;
        if(this.articles.size()  > ITEMS_LIMIT){
            return random.nextInt(ITEMS_LIMIT);
        } else {
            return this.articles.size();
        }
    }

Could somebody help me solve this?




Aucun commentaire:

Enregistrer un commentaire