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