我的简单的基于firebase的应用程序有点问题。我试图从数据库中检索数据,如下所示:
public void readbuttonposition() {
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot buttonSnapshot: dataSnapshot.getChildren()){
ButtonPosition buttonPosition = buttonSnapshot.getValue(ButtonPosition.class);
String user = buttonPosition.getbutton_position();
if (user == "1"){
button2.setVisibility(View.GONE);
button3.setVisibility(View.VISIBLE);
}
else if (user == "0"){
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.GONE);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
只需有两个按钮,每个按钮都会给出值:0和1.如您所见,当button2
为VISIBLE
时,button3
为GONE
。按下按钮,一切都很好。但是,当我想重新启动应用程序并再次启动它时,总是button2
可见,即使在数据库时也是如此
button_position的值为1,应该button3
可见..这里是firebase树的屏幕截图
和DatabaseReference
路径:
database = FirebaseDatabase.getInstance().getReference("users").child(user.getUid()).child("hours");
即时通讯也以这种方式检索start_hour
,它运行正常,所以我失败了? :d
@edit ButtonPosition.class
代码
public class ButtonPosition {
String button_position;
public ButtonPosition(){
}
public ButtonPosition(String button_position) {
this.button_position = button_position;
}
public String getbutton_position(){
return button_position;
}
}
答案 0 :(得分:2)
正如Bob在评论中所说,在比较字符串时,您需要使用equals()
方法,例如:if(users.equals("x")){...}
并在你的班级中添加二传手: -
public class ButtonPosition {
String button_position;
public ButtonPosition(){
}
public ButtonPosition(String button_position) {
this.button_position = button_position;
}
public void setButtonPosition(String button_position){
this.button_position = button_position;
}
public String getButtonPosition(){
return button_position;
}
}