1

i have a little problem with my simple firebase based app. Im trying to retrieve data from database like this:

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) {

            }
        });
    }

simply there are two buttons, each of them gives values: 0 and 1. As you can see, when button2 is VISIBLE then button3 is GONE. Thats allright everything is fine when im pressing the buttons. But when i want to restart the app and launch it again there's always button2 visible, even when database button_position is valued as 1 which should make button3 visible.. Here's a screenshot of firebase tree screenshot and DatabaseReference path: database = FirebaseDatabase.getInstance().getReference("users").child(user.getUid()).child("hours"); im also retrieving start_hour this way and it works fine, so where i failed ? :D

@edit ButtonPosition.class code

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;
    }
}
Shizzle
  • 53
  • 1
  • 13
  • 2
    Jave string comparison uses the `equals()` method not `==`. Change `user == "X"` to `user.equals("X")`. You should also post your `ButtonPosition` class to allow us to confirm that you have defined the getter/setter methods correctly. – Bob Snyder Jan 26 '18 at 17:23
  • @BobSnyder added – Shizzle Jan 26 '18 at 17:26

1 Answers1

2

As Bob said in the comments when comparing strings you need to use the equals() method, so example: if(users.equals("x")){...}

and in your class add the setters:-

 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;
  }
}
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • when im using .equals(), i got NullPointerException, how can i solve this? :P – Shizzle Jan 26 '18 at 17:35
  • @Shizzle you need to do like this `String x="1";`. `String user="users";` `if(user.equals(x){...}`, so something like this – Peter Haddad Jan 26 '18 at 17:42
  • did it like u said, `String x = "1"` and `if (user.equals(x))` and still got NullPointException, honestly i dont really know how to solve that.. – Shizzle Jan 26 '18 at 17:55
  • put a breakpoint next to this line `String user = buttonPosition.getbutton_position();` – Peter Haddad Jan 26 '18 at 17:58
  • String user = buttonPosition.getbutton_position() so user is 0 or 1, right now its "0" in firebase – Shizzle Jan 26 '18 at 18:00
  • @Shizzle one of the variables is null.. change this in the model class `getbutton_position` to this `getButtonPosition` – Peter Haddad Jan 26 '18 at 18:03
  • still got NullPointerException after changing to `getButtonPosition` – Shizzle Jan 26 '18 at 18:07
  • you need to add a breakpoint next to user to be sure that it is retrieving. Put a condition before it `if(user!=null){ if(user.equal(x)){...}}` to see if it enters it and add Log.i("it entered:", user); – Peter Haddad Jan 26 '18 at 18:14
  • nothing happend, so i guess it didn't entered. – Shizzle Jan 26 '18 at 18:25
  • yes means it is not retrieving anything did you change this `buttonPosition.getButtonposition(); and in model class – Peter Haddad Jan 26 '18 at 18:28
  • Oh, previously i set it as `getButtonPosition`, now i changed it to `getButtonposition` and it works. Thank you very much Peter :) – Shizzle Jan 26 '18 at 18:34