Retrieving Data in Firebase Android with Class

0

I have this bank structure that is this

InmyclassIhaveafunctioncalledgetLocal(),whereitwouldtakethenameIguatemi.ofthisstructure.Butwhenperformingthequeryitisreturningasnull.

Followmyclasswiththefunctionsbelow:

publicclassMesa{privateStringID;privateStringLocal;privateDatabaseReferencereference;privateStringNFC;publicMesa(){}publicvoidSalvar(){DatabaseReferencereference=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 better the question I'll put as I'm 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.model.Mesa;

import static meals.com.meals.R.array.Restaurante_KFC_Pratos; import static meals.com.meals.R.array.Restaurante_McDonalds_Pratos;

public class Make Request 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);



}

}

    
asked by anonymous 29.09.2017 / 01:55

1 answer

1

Call dataSnapshot.getValue() to access the data. And it's good practice to check dataSnapshop.exists() before calling.

If it does not exist, it is because you are accessing an 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 save and return data through Firebase, create a simple Getter for all variables that you want to retrieve. Just like with ID, but with Location too.

Just like you have a child method, create a public void Salvar() that will download the table just like you did. Return to 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 location of the Table class.

public String getLocal() {
    return local;
}

So, if you want to access the location, just call public Mesa Recuperar() and do what you want, such as saving to a global variable or returning.

    
29.09.2017 / 02:52