不能从firebase中重新布尔
如果这是一个非常基本的问题,我很抱歉,但我无法弄清楚这个问题。 我试图根据 Firebase 数据库中的布尔值实现 if 语句。我尝试过各种组合,但我最近的尝试如下。
当我使用以下代码运行应用程序时:
- 没有任何反应
- 没有错误消息
- 显示“likeButtonNotClicked”按钮(对于 true 和 false 条件)
RetreiveLikeReference = FirebaseDatabase.getInstance().getReference().child("Main Wall Posts").child(postId).child("liked")
private void UpdateLike() {
if(RetreiveLikeReference.equals(true)){
likeButtonClicked.setVisibility(VISIBLE);
likeButtonNotClicked.setVisibility(GONE);
} else {
likeButtonClicked.setVisibility(GONE);
likeButtonNotClicked.setVisibility(VISIBLE);
}
}
我也尝试了下面的代码并得到了相同的结果。
RetreiveLikeReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
snapshot.getValue();
if (snapshot.equals(true)){
likeButtonClicked.setVisibility(VISIBLE);
likeButtonNotClicked.setVisibility(GONE);
} else {
likeButtonClicked.setVisibility(GONE);
likeButtonNotClicked.setVisibility(VISIBLE);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
I'm sorry if this is a very basic question, but I am not able to figure out this issue.
I am trying to do achieve an if statement based on a boolean value in the Firebase database. I have tried various combinations, but my most recent attempt is below.
When I run the app with the below code:
- Nothing happens
- There is no error message
- The "likeButtonNotClicked" button displays (for both true and false conditions)
RetreiveLikeReference = FirebaseDatabase.getInstance().getReference().child("Main Wall Posts").child(postId).child("liked")
private void UpdateLike() {
if(RetreiveLikeReference.equals(true)){
likeButtonClicked.setVisibility(VISIBLE);
likeButtonNotClicked.setVisibility(GONE);
} else {
likeButtonClicked.setVisibility(GONE);
likeButtonNotClicked.setVisibility(VISIBLE);
}
}
I also tried the code below and got the same result.
RetreiveLikeReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
snapshot.getValue();
if (snapshot.equals(true)){
likeButtonClicked.setVisibility(VISIBLE);
likeButtonNotClicked.setVisibility(GONE);
} else {
likeButtonClicked.setVisibility(GONE);
likeButtonNotClicked.setVisibility(VISIBLE);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
boolean.valueof()
方法从数据库中获取布尔值。您不是告诉Java所获得的值类型,因此Java将值的类型自动设置为Object
。您可以通过告诉Java的值类型为
boolean
,可以通过:Try getting the boolean value from your database using the
Boolean.valueOf()
method. You're not telling java the type of value that you are getting so java sets automatically the type of the value toObject
.You can fix it by telling to java that the value type is
Boolean
like this: