Recovering Data on Firebase Android with Class

Asked

Viewed 3,704 times

0

I have this bank structure that is this

inserir a descrição da imagem aqui

In my class I have a function called getLocal (), where she would take the name Iguatemi. of this structure. However when performing the query it is returning as null.

Follow my class with the functions below:

public class Mesa {

private String ID;
private String Local;
private DatabaseReference reference;
private String NFC;

public Mesa() {
}

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(String Txt) {
    DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
    reference.child("Mesas").child(Txt).child("local");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String texto = (String) dataSnapshot.getKey();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("TAG", databaseError.getMessage());
        }
    });

        return Local;
}

public void setLocal(String local) {
    Local = local;
}

}

Please help me with what I’m missing.

To get the question better I will put as I am doing in Activity.

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.view.View; import android.widget.Adapterview; import android.widget.Arrayadapter; import android.widget.Spinner; import android.widget.Textview;

import Meals.com.Meals. R; import Meals.com.Meals.activity.modelo.Mesa;

import Static Meals.com.Meals.R.array.Restaurante_kfc_pratos; import Static Meals.com.Meals.R.array.Restaurante_mcdonalds_dishes;

public class Effectedido extends Appcompatactivity {

private TextView InfoMesa;
private Spinner SpinnerRestaurante;
private Spinner SpinnerPratos;
private Mesa Mesas;

@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);
    SpinnerPratos = (Spinner) findViewById(R.id.spinner_pratos);

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    String NFC = bundle.getString("TagNFC");

    Mesa Mesas = new Mesa();
    Mesas.RecuperarMesa(NFC);

    InfoMesa.setText("Você esta em " + Mesas.getLocal() + " " + "na mesa \n" + NFC);
   // InfoMesa.setText("Você esta na na mesa \n" + NFC);



}

}

1 answer

1


Call dataSnapshot.getValue()to access the data. And it is good practice to check whetherdataSnapshop.exists() before calling.

If it doesn’t exist, it’s because you’re accessing a child empty.

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshop.exists()){
            Mesa mesa = dataSnapshot.getValue(Mesa.class);

            //Faça o que quiser com o objeto.
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e("TAG", databaseError.getMessage());
    }
});

Whenever you are saving and returning data through Firebase, create a simple Getter for all the variables you are going to recover. Just like you did with ID, but with Local too.

Just as you have a method public void Salvar(), create a public Mesa Recuperar() that will lower the Table just as you did. Return this Table.

public Mesa Recuperar(){
    Mesa mMesa = new Mesa();
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshop.exists()){
                mMesa = dataSnapshot.getValue(Mesa.class);

                //Faça o que quiser com o objeto.
            }
        }
    return mMesa;
}

And having the simple getter for the site of the Table class.

public String getLocal() {
    return local;
}

So if you want to access the site, just call mMesa.getLocal() and do whatever you want, like saving to a global variable or return.

  • If I want to take the Name of the place in String and return, how would that look? Because how am I getting The Table class.class I would get it all right right?

  • 1

    The "getLocal()" method should be a common getter method. And then you could call the.getLocal() table to have access to the String returned by it. And recover the database data through a separate method, which would return a Mesa.

  • I think I’m really dumb, I don’t know if you can pass an email or something to help me more..... follow my ai 19 997749701 can call me

  • Inside the Recover Table I put this part of the code too because I have to search through the ID public Mesa RecuperarMesa (String Txt){ Mesa mesa = new Mesa(); DatabaseReference reference =ConfiguracaoFirebase.getReferencia(); reference.child("Mesas").equalTo(Txt);

  • From what your code shows, you saved the table using the ID. So to search using the ID just give a Reset.Child("Tables"). Child(ID). With this you already have the table reference with the ID you want.

Browser other questions tagged

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