从Firebase数据库检索计数数据 [英] Retrieve count data from Firebase database
本文介绍了从Firebase数据库检索计数数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据库.我需要的是发布 UID
等于current_user_uid时获得每个帖子的评论总数.
I have the following database. What I need is to get the total count of comments per post when posting UID
equal to current_user_uid.
关于如何执行此操作的任何建议?
Any suggestion on how to do this?
推荐答案
要解决此问题,请使用以下代码:
To solve this, please use the following code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Posts").orderByChild("uid").equalTo(current_user_uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
long count = ds.child("comments").getChildrenCount();
Log.d("TAG", "Count: " + count);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);
这篇关于从Firebase数据库检索计数数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文