Why my Firebase addvalueEventlistener won't get called?

Viewed 409

Currently, I am writing a social media app, and now I would like to retrieve the datas from my firebase database, where each user has some ratings. mUploads - it stores the uploaded items (photo, name, city, ID). mDatabaseRef-ratings - it stores the ratings of the uploaded items, (some items don't have ratings yet). They have the same ID, just like in the mUploads->ID

It's actually a big project, and I have retrieved multiple times other type of firebase elements, but this time the addvalueventlistener won't get called, and I couldn't figure it out. Why?

It's not the whole code, but the main part is this. I tried to several ways, but nothing helped. I tried call it not only in this adapter class, but also in my other class, where I retrieve the data for the mUploads list.

...

public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.ImageViewHolder> implements Filterable {
    private Context mContext;
    private List<Upload> mUploads;
    private List<Upload> exampleListFull;
    private OnNoteListener mOnNoteListener;
    private ArrayList<Ratingsclass> mratings_array;
    public float sumofstars, numofratings, defaultstars;
    private DatabaseReference mDatabaseRef_ratings;

    public ProfileAdapter(Context context, List<Upload> upload, OnNoteListener onNoteListener){//, ArrayList<Ratingsclass> ratinglist){
        mContext=context;
        mUploads=upload;
        exampleListFull=new ArrayList<>(upload);
        mOnNoteListener=onNoteListener;

        for (int i = 0; i < mUploads.size(); i++){
            mDatabaseRef_ratings = FirebaseDatabase.getInstance().getReference("ratings");
            Log.d("myTag", "loop");
            /*checking if child already exists*/
            mDatabaseRef_ratings=mDatabaseRef_ratings.child(mUploads.get(i).getKey());
            Log.d("myTag", "ez az id: "+ mUploads.get(i).getKey());

            final int finalI = i;
            mDatabaseRef_ratings.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() == null) {
                        // The child doesn't exist
                        Log.d("myTag", "has no rating");
                    }else{
                        mDatabaseRef_ratings = FirebaseDatabase.getInstance().getReference("ratings").child(mUploads.get(finalI).getKey());
                        Log.d("myTag", "checking the elements of this child");
                        mDatabaseRef_ratings.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                Ratingsclass temp = new Ratingsclass();
                                numofratings = 0;
                                sumofstars = 0;
                                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                                    temp = postSnapshot.getValue(Ratingsclass.class);
                                    numofratings += 1;
                                    sumofstars += temp.getNum_of_stars();
                                    Log.d("myTag", numofratings + " es " + sumofstars);
                                }
                                if (numofratings == 0)
                                    defaultstars = 0;
                                else
                                    defaultstars = sumofstars / numofratings;
                                temp.setRatingid(mUploads.get(finalI).getKey());
                                temp.setRating_sum(defaultstars);
                                mratings_array.add(temp);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    //
                }
            });

        }

    }   

... etc

2 Answers

If you pass a listener to the constructor of your adapter onNoteListener you should be listening in your activity/fragment for changes that this interface does.

In your code, inside your adapter you use

mOnNoteListener=onNoteListener;

but you never call the interface to send the values to your view (inside onDataChange(...))

so, what you need to do to get these values is to call your interface to send these values to your view

so, after you fetch the data in Firebase use your callback

mOnNoteListener.yourMethod(your_value);

I could manage to solve, with the help of another topic. I just added this code:

mDatabaseRef_ratings.child("ratings").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (final DataSnapshot entrySnapshot : dataSnapshot.getChildren()) {
                    alreadyrated=false;
                    for (DataSnapshot propertySnapshot : entrySnapshot.getChildren()) {
                        Log.d("myTag", entrySnapshot.getKey() + " : " + propertySnapshot.getKey());

... etc

This double for loop is enough to get the id of the parent and the id of the child too, so now my rating app is working.

Related