0
Hello folks I’m having a problem to perform query in firebase database.
This is the bank:
I’m trying to carry out a search of all the restaurants that have the name Iguatemi, until then according to my debug, I’m managing to make it good.
The problem is that when it comes time to take the die, it’s giving back as null
.
Here’s a screenshot of Debug:
Follows the code:
package meals.com.meals.activity.Activity;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import meals.com.meals.R;
import meals.com.meals.activity.Config.ConfiguracaoFirebase;
import meals.com.meals.activity.modelo.Mesa;
import meals.com.meals.activity.modelo.Restaurante;
import static meals.com.meals.R.array.Restaurante_KFC_Pratos;
import static meals.com.meals.R.array.Restaurante_McDonalds_Pratos;
public class FazerPedido extends AppCompatActivity {
private TextView InfoMesa;
private Spinner SpinnerRestaurante;
private Spinner SpinnerPratos;
private Mesa Mesas;
private DatabaseReference reference;
private Restaurante restaurante;
private String Local;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fazer_pedido);
InfoMesa = (TextView) findViewById(R.id.textViewInfoMesa);
SpinnerRestaurante = (Spinner) findViewById(R.id.spinnerRestaurante);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
final String NFC = bundle.getString("TagNFC");
Mesa mesas = new Mesa();
// Restaurante restaurante = new Restaurante()
reference = ConfiguracaoFirebase.getReferencia().child("Mesas").child(NFC);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Mesa mesas = dataSnapshot.getValue(Mesa.class);
final String local = mesas.getLocal().toString();
InfoMesa.setText("Você esta em " + local + " " + "na mesa \n" + NFC);
// Após ler o NFC ele faz a busca do local
Query query = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("localRestaurante").equalTo(local);
// outra referencia porem apontando para restaurante
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
Restaurante restaurante = dataSnapshot1.getValue(Restaurante.class);
final String local = restaurante.getLocalRestaurante();
final String nomerest = restaurante.getNome();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
In the first search I did to return the location of the table only deleting the node table and adding again to tables I could get the return, I tried to perform the same procedure but there was no change.
I will also leave the two classes of my objects to get a better sense:
Restaurant.class
public class Restaurante {
private String Nome;
private String LocalRestaurante;
private String ID;
private DatabaseReference reference;
public Restaurante() {
}
public void Salvar(){
DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
reference.child("Restaurante").child(getID()).setValue(this);
}
public String getNome() {
return Nome;
}
public void setNome(String nome) {
Nome = nome;
}
public String getLocalRestaurante() {
return LocalRestaurante;
}
public void setLocalRestaurante(String localRestaurante) {
LocalRestaurante = localRestaurante;
}
@Exclude
public String getID() {
return ID;
}
public void setID() {
reference = ConfiguracaoFirebase.getReferencia().child("Restaurante");
this.ID = reference.push().getKey();
}
}
Mesa.class
public class Mesa {
private String ID;
private String Local;
private String NFC;
private DatabaseReference reference;
public void Salvar(){
DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
reference.child("Mesas").child(getID()).setValue(this);
}
@Exclude
public String getID() {
return ID;
}
public void setID() {
reference = ConfiguracaoFirebase.getReferencia().child("Mesas");
this.ID = reference.push().getKey();
}
public String getLocal() {
return Local;
}
public void setLocal(String local) {
Local = local;
}
}
One thing that I found very strange too, is that firebase let me use the table class without its builder.
I got here discovered the error. I will post the answer
– IMoonBlackI