How do I search (SELECT and WHERE) in the firebase database?

Asked

Viewed 4,128 times

3

I have a database like this:

{
  "message" : "Hello, World!",
  "reserva" : [ null, {
    "codreserva" : 123,
    "email" : "[email protected]",
    "status" : "check in"
  }, {
    "codreserva" : 124,
    "email" : "[email protected]",
    "status" : "check out"
  } ]
}

And I have a code to check the status of the reservation:

public void ProcuraReserva(String codigodareserva){
// Read from the database
myRef = database.getReference("reserva/"+codigodareserva+"/status");
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String value = dataSnapshot.getValue(String.class);
        textview1.setText("Status da reserva "+ value);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        textview1.setText("Status com falha");
    }
  });
}

However, he search the ID of my reservation wanted him to search according to the CODRESERVA. Could someone help me in this matter search according to my "line" in this database of Firebase.

2 answers

2


Try to use Child()

Firebase Child

myRef.child("codreserva").child(codigoreserva).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    String value = dataSnapshot.getValue(String.class);
    textview1.setText("Status da reserva "+ value);
}

@Override
public void onCancelled(DatabaseError error) {
    textview1.setText("Status com falha");
}
  • You didn’t bring anything by doing this.

  • I think the people did not understand given the codreserva = 123 I want to return her status.

  • 1

    Try to use the equalTo() to compare Child’s value. mCadastroRef.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {... mine works like this

0

What noble Fabio said, all right. N forgets q you have to pass the email parameter that is inside .equalTo. To receive values, in your case, I recommend you to do:

  String value = dataSnapshot.child("nome").getValue().toString();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.