mardi 29 juin 2021

How can I generate a random integer in RecyclerView Adapter?

I need random number in recyclerView adapter but I am unable to get any. I am trying to generate the number in the Activity.java and pass it in the constructor but I am getting same number on every item. I have tried this :

                Random random = new Random();
                randInt = random.nextInt(6);
                adapterPost = new AdapterPost(QuestionsActivity.this, postList, randInt);

//Adapter

public class AdapterPost extends RecyclerView.Adapter<AdapterPost.ViewHolder> {

Context context;
ArrayList<ModelPost> postList;
String myUid;
DatabaseReference likesRef;
DatabaseReference postRef;
boolean hasLiked = false;

int randInt;

public AdapterPost(Context context, ArrayList<ModelPost> postList, int randInt) {
    this.context = context;
    this.postList = postList;
    this.randInt = randInt;
    myUid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    likesRef = FirebaseDatabase.getInstance().getReference("Likes");
    postRef = FirebaseDatabase.getInstance().getReference("Posts");

}

Please let me know how can I get a different random number on evey item. Thanks!




Aucun commentaire:

Enregistrer un commentaire