无法在 onDataChange 中获取值
Can't getValue inside onDataChange
我尝试在我的应用程序上从我的 firebase 实时数据库中获取一个值,但我无法使用 .getValue()
来获取我 [=19= 上的 post 个赞的值] 因为某些原因。我已经尝试在另一个 class 上进行另一个引用,但我仍然无法使用 getValue()
方法。
我的部分代码:
public static void setIconToLiked(String postID) {
likeRef.addValueEventListener(new ValueEventListener() {
public ImageSwitcher like_btn;
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
if (snapshot.child(postID).hasChild(HomeActivity.userID)){
// user has been liked this so we need to change the icon of the button to liked \
like_btn.setImageResource(R.drawable.like_emoji);
// here i can't get the value of the post likes \
postRef.child(postID).child("likes").getValue().toString();
}else{
// user has not liked this post so we need to set the icon of the button to not-liked \
like_btn.setImageResource(R.drawable.like_emoji_before);
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
}
});
}
您只能在 database reference
对象上使用 getValue
方法
我尝试在我的应用程序上从我的 firebase 实时数据库中获取一个值,但我无法使用 .getValue()
来获取我 [=19= 上的 post 个赞的值] 因为某些原因。我已经尝试在另一个 class 上进行另一个引用,但我仍然无法使用 getValue()
方法。
我的部分代码:
public static void setIconToLiked(String postID) {
likeRef.addValueEventListener(new ValueEventListener() {
public ImageSwitcher like_btn;
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
if (snapshot.child(postID).hasChild(HomeActivity.userID)){
// user has been liked this so we need to change the icon of the button to liked \
like_btn.setImageResource(R.drawable.like_emoji);
// here i can't get the value of the post likes \
postRef.child(postID).child("likes").getValue().toString();
}else{
// user has not liked this post so we need to set the icon of the button to not-liked \
like_btn.setImageResource(R.drawable.like_emoji_before);
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
}
});
}
您只能在 database reference
对象上使用 getValue
方法