Query result null Firebase

Asked

Viewed 514 times

0

Hello folks I’m having a problem to perform query in firebase database.

This is the bank:

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

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.

2 answers

0

You’re trying to turn into an object where restaurant is a list

Try this

 @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();
                    }
                }
  • I got here discovered the error. I will post the answer

0

The problem of not returning is that I am using a wrong firebase function to make it work should be used query.addChildEventListener(new Childeventlistener() or query.addListenerForSingleValueEvent(new Valueeventer listener()

so the code stayed 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) {

        }
    });
  • the problem is not that, the problem is that Voce was not going through a list...

  • so much so that if you continue using . addChildEventListener and scroll through the list will be the same result.

  • @Edsonreis I tried to use but it didn’t work when I used single worked right

Browser other questions tagged

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