Reputation: 39
I'm having trouble getting any values from my firebase database for android using the documentation that was given. I've tried connecting to the simplest structure possible but still no luck. Below is my code.
DatabaseReference usersReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
usersReference = FirebaseDatabase.getInstance().getReference("users");
...
}
protected void onStart() {
super.onStart();
System.out.println("User Reference: "+usersReference.toString());
usersReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.child("name").getValue(String.class);
mongo.setText(value);
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
Here is what my database looks like in firebase
For some reason no matter what I try I get nothing. I tried printing the user reference to make sure I was going to the correct place, and although the link was the correct reference to the database when I clicked the link the I was met with this screen:
Not sure if that is relevant or not. Thank you
Upvotes: 0
Views: 87
Reputation: 598728
From the rest of the behavior you're describing it seems like your code is looking at a different database from what's shown in the screenshot. I recommend re-downloading the google-services.json
for your project, and adding it to the project again.
Upvotes: 1
Reputation: 840
When that happens, you better use the debugger to understand what's going on. Are you using Android Studio right?
Put a breakpoint on the following line:
String value = dataSnapshot.child("name").getValue(String.class);
And then, when the program execution stops here, click the "evaluate expression" button. Inside that window you can experiment with dataSnapshot. I suggest you to try dataSnapshot.getChildren() there and see what does it show.
Upvotes: 0