Search by name with firebase in android

I have a problem several days ago and I don't find a solution.

I´m trying search in my firebase database by name but the result is always null.

The database is the next;

database

The code that I use is the next;

final String nombre = edtNombreJugador.getText().toString().trim().toUpperCase();

mDatabase.child("user").child("personalData").orderByChild("name").equalTo(nombre);

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){

String title = (String) singleSnapshot.child("name").getValue(String.class);
System.out.println("TITLE: "+title);


 }

Title always return null in my case.

Can somebody help me please? I don't know what I'm doing bad...

Upvotes: 4

Views: 3389

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

Right now you're querying the database path /user/personalData. And then for each child node under there, you're comparing the name property to the value the user entered. So /user/personalData/***/name.

That doesn't match the path in the database, which is /user/***/personalData/name. To query that you do:

Query query = mDatabase.child("user").orderByChild("personalData/name").equalTo(nombre);

query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {...

Upvotes: 4

Im trying too with this code (more clear for me) for return all names in database but the result is null too:

 DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("/users/");
 mDatabase.child("personalData");
  final UsersData[] user = {null};
  ValueEventListener postListener = new ValueEventListener() {
    @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
     for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
         String title = (String) 
         postSnapshot.child("name").getValue(String.class);
          System.out.println("TITLE: "+title);
       }
     }
      @Override
       public void onCancelled(DatabaseError firebaseError) {
       Log.e("The read failed: " ,firebaseError.getMessage());
       }
    };
    mDatabase.addValueEventListener(postListener);

Upvotes: 0

Related Questions