0

Firebase query returns value for this query

Query query = databaseReference.child("users").orderByChild("display_name").equalTo(s.toString());

{nfxQ14lxSIZ0qgTGllZ297b0nTv2={display_name=Friends, token= token_value}}

The item is caught by the model class

        query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            UserInfo userInfo = snapshot.getValue(UserInfo.class);
            userInfo.getDisplay_name()
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

However, returns null

Model class

public class UserInfo {

@SerializedName("display_name")
private String display_name;
@SerializedName("token")
private String token;

public UserInfo() {}

public String getDisplay_name() {return display_name;}

public void setDisplay_name(String display_name) { this.display_name = display_name;}

public String getToken() {return token;}

public void setToken(String token) {this.token = token;}
}

Database structure is as follows

enter image description here

I am following the logic from this question

How to Convert Firebase data to Java Object...?

Yet I am still getting a null result once the query result has been converted

I believe the problem is that when I request the key I am getting back users and not the uid or one step below the display_name

Emmanuel Conradie
  • 345
  • 1
  • 5
  • 20

1 Answers1

0

Here the solution is quite simple

The ValueEventListener should be a ChildEventListener

In this way the snapshot.getKey() returns the child value and therefore the lower key is accessible by using

String text = (String) snapshot.getChild("some_lower_key").getValue();
String diffOption = snapshot.getValue(Model.class);

But the main thing is to use a ChildEventListener when using a query to Firebase

Emmanuel Conradie
  • 345
  • 1
  • 5
  • 20