[英]Why my Firebase addvalueEventlistener won't get called?
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.目前,我正在编写一个社交媒体应用程序,现在我想从我的 firebase 数据库中检索数据,其中每个用户都有一些评分。 mUploads - it stores the uploaded items (photo, name, city, ID).
mUploads - 它存储上传的项目(照片、姓名、城市、ID)。 mDatabaseRef-ratings - it stores the ratings of the uploaded items, (some items don't have ratings yet).
mDatabaseRef-ratings - 它存储上传项目的评级,(有些项目还没有评级)。 They have the same ID, just like in the mUploads->ID
它们具有相同的 ID,就像在 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.这实际上是一个大项目,我已经多次检索其他类型的 firebase 元素,但是这次 addvalueventlistener 不会被调用,我想不通。 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.
我尝试不仅在这个适配器 class 中调用它,而且在我的其他 class 中调用它,我在其中检索 mUploads 列表的数据。
... ...
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 ... ETC
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.如果您将侦听器传递给适配器
onNoteListener
的构造函数,您应该在您的活动/片段中侦听此接口所做的更改。
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(...))但是您从不调用接口将值发送到您的视图(在 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因此,在您获取 Firebase 中的数据后,请使用您的回调
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 ... 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.这个双 for 循环足以获取父母的 id 和孩子的 id,所以现在我的评分应用程序正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.