Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 467
Antonio Herrero Martínez Avatar asked Nov 15 '25 01:11

Antonio Herrero Martínez


1 Answers

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) {...
like image 112
Frank van Puffelen Avatar answered Nov 17 '25 22:11

Frank van Puffelen