Skip to content Skip to sidebar Skip to footer

Get Random Data Android Firebase Database In Recyclerview

Firebase database In child('AdUsersINFO') I have a childs for users (userid) in this childs we have a posts . My goal was to get a random post and display the data in RecyclerView

Solution 1:

Your DatabaseReference is wrong. You need to change this line of code:

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");

with

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO").child(uid);

In which the uid is the user id of the logged-in user. To get the that uid, please use this line of code:

Stringuid= FirebaseUser.getInstance().getCurrentUser().getUid();

Whith your code, when you are iterating using getChildren() method, you iterating through users not posts as I understand you intend to do.

Solution 2:

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");
mAdUserDatabse.addValueEventListener(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        intads= (int) dataSnapshot.getChildrenCount();
        intrand=newRandom().nextInt(ads);
        for(DataSnapshot data: dataSnapshot.getChildren()){
        for(DataSnapshot datas: data.getChildren()){
        for(inti=0; i < rand; i++) {

        list = newArrayList<Adver>();

            Advervalue= datas.getValue(Adver.class);
            Adverfire=newAdver();
            Stringtype= value.getType();
            Stringcategories= value.getCategories();
            Stringcity= value.getCity();
            fire.setType(type);
            fire.setCategories(categories);
            fire.setCity(city);
            list.add(fire);

}}}}

Post a Comment for "Get Random Data Android Firebase Database In Recyclerview"