Query with result null Firebase

0

Hello everyone, I'm having a problem querying the firebase database.

The bank is this:

I'mtryingtodoasearchforalltherestaurantsthathavethenameIguatemi,untilthenaccordingtomydebug,I'mgettingtodoitgood.

Theproblemisthatwhenthedataistaken,itisreturningasnull.

FollowDebugimage:

Followthecode:

packagemeals.com.meals.activity.Activity;importandroid.app.Activity;importandroid.content.Intent;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ArrayAdapter;importandroid.widget.Spinner;importandroid.widget.TextView;importcom.google.firebase.database.ChildEventListener;importcom.google.firebase.database.DataSnapshot;importcom.google.firebase.database.DatabaseError;importcom.google.firebase.database.DatabaseReference;importcom.google.firebase.database.FirebaseDatabase;importcom.google.firebase.database.Query;importcom.google.firebase.database.ValueEventListener;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importmeals.com.meals.R;importmeals.com.meals.activity.Config.ConfiguracaoFirebase;importmeals.com.meals.activity.modelo.Mesa;importmeals.com.meals.activity.modelo.Restaurante;importstaticmeals.com.meals.R.array.Restaurante_KFC_Pratos;importstaticmeals.com.meals.R.array.Restaurante_McDonalds_Pratos;publicclassFazerPedidoextendsAppCompatActivity{privateTextViewInfoMesa;privateSpinnerSpinnerRestaurante;privateSpinnerSpinnerPratos;privateMesaMesas;privateDatabaseReferencereference;privateRestauranterestaurante;privateStringLocal;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_fazer_pedido);InfoMesa=(TextView)findViewById(R.id.textViewInfoMesa);SpinnerRestaurante=(Spinner)findViewById(R.id.spinnerRestaurante);Intentintent=getIntent();Bundlebundle=intent.getExtras();finalStringNFC=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 made to return the table location only by deleting the table node and adding tables again I was able to get the return, I tried to perform the same procedure but there was no change.

I will also leave both classes of my objects to have a better idea:

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 the firebase have let me use the table class without its constructor.

    
asked by anonymous 03.10.2017 / 15:20

2 answers

0

You are trying to turn into an object and that restaurant is a list

Try this out

 @Override
 public void onDataChange(DataSnapshot dataSnapshot1) {

               for (DataSnapshot postSnapshot: dataSnapshot1.getChildren()) {

                        Restaurante restaurante = postSnapshot.getValue(Restaurante.class);
                        final String local = restaurante.getLocalRestaurante();
                        final String nomerest = restaurante.getNome();
                    }
                }
    
03.10.2017 / 16:09
0

The problem of not returning is that I'm using a wrong firebase function to make it work query.addChildEventListener (new ChildEventListener () or query.addListenerForSingleValueEvent (new ValueEventListener ()

so the code got this way

query query = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("localRestaurante").equalTo(local);

            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    final List<String> areas = new ArrayList<String>();
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                        Restaurante restaurante = data.getValue(Restaurante.class);
                        //final String local = restaurante.getLocalRestaurante();
                        //final String nomerest = restaurante.getNome();

                        areas.add(restaurante.getNome().toString());
                    }
                    SpinnerPratos = (Spinner) findViewById(R.id.spinnerRestaurante);
                    ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, areas);
                    areasAdapter.setDropDownViewResource(R.layout.spinner_item);
                    SpinnerPratos.setAdapter(areasAdapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    
03.10.2017 / 16:36