how to show saved data in Firebase database with android studio

0

Friends I'm doing a project in Firebase database with android and I need to consult and get the last three values saved with the name of "dose" this is the structure in Firebase

This is my user.java class

public class usuario {
private String email;
private  String dosis;
private String id;
private int dia;
private int mes;
private int año;

public usuario(String email, String dosis, String id, int dia, int mes, int año) {
    this.email = email;
    this.dosis = dosis;
    this.id = id;
    this.dia = dia;
    this.mes = mes;
    this.año = año;
}
public usuario() {

}

public int getDia() {
    return dia;
}

public void setDia(int dia) {

............

This is how I'm getting the value in an activity and inside a button I have

my.orderByChild("usuario").limitToLast(3).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                        usuario us =dataSnapshot.getValue(usuario.class);
                    textViewcalculo.setText(us.getDosis());

                }

but in the debug I get this by pressing the button on which I'm making the call.

Someone who can tell me if it is the right way or if there is another and what would it be? Thanks.

    
asked by Julian 05.10.2018 в 01:20
source

1 answer

0

Try this

    my.child("usuario").orderByKey().limitToLast(3).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                                for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                               usuario us =snapshot.getValue(usuario.class);
                            textViewcalculo.setText(us.getDosis());
                            Log.e("Datos: " , "" + us.getDosis());
                                    }


                        }

remember that being a textview will set only the last value for the for, but you can put a log and see if it brings you the 3

    
answered by 05.10.2018 / 02:03
source