Montero
Montero

Reputation: 31

how i recover data from firebase to android?

I want read data, to build a object that I can display for my project.

database

public class Curp{

  public String curpGen, Nombre, ApPat, apMat, sexo, estado, fecha;

  public Curp(String  curpGen,String Nombre,String ApPat,String apMat,String fecha,String sexo,String estado){
    this.curpGen=curpGen;
    this.Nombre=Nombre;
    this.ApPat=ApPat;
    this.apMat=apMat;
    this.fecha=fecha;
    this.sexo=sexo;
    this.estado=estado;
  }

}

In this part, I have the methods to read the data and save to create the object curp.

I want know how build the object Curp with data.

 database.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String curpGen=dataSnapshot.getValue(Curp.class).curpGen.toString();
            String nombre=dataSnapshot.getValue(Curp.class).Nombre.toString();
            String ApPat=dataSnapshot.getValue(Curp.class).ApPat.toString();
            String ApMat=dataSnapshot.getValue(Curp.class).apMat.toString();
            String Fecha=dataSnapshot.getValue(Curp.class).fecha.toString();
            String sexo=dataSnapshot.getValue(Curp.class).sexo.toString();
            String edo=dataSnapshot.getValue(Curp.class).estado.toString();

            Curp value=new Curp(curpGen,nombre,ApPat,ApMat,Fecha,sexo,edo);
            lista.add(value);
            cupadapter=new CurpAdapter(lista);
            reciclador.setAdapter(cupadapter);
        }
 }

22:33:01.533 13269-13269/com.example.montero.softtimcurpmontero E/RecyclerView: No adapter attached; skipping layout 10-10

22:33:01.763 13269-13269/com.example.montero.softtimcurpmontero E/RecyclerView: No adapter attached; skipping layout 10-10

22:33:01.953 13269-13269/com.example.montero.softtimcurpmontero E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.montero.softtimcurpmontero, PID: 13269 com.google.firebase.database.DatabaseException: Class com.example.montero.softtimcurpmontero.Curp is missing a constructor with no arguments

Upvotes: 0

Views: 126

Answers (2)

Montero
Montero

Reputation: 31

The only thing that i need was build a empty condtructor in class Curp an Curp value in MainActivity

database.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Curp value=new Curp();
            value=dataSnapshot.getValue(Curp.class);
            lista.add(value);
            cupadapter=new CurpAdapter(lista);
            reciclador.setAdapter(cupadapter);
        }

}

Upvotes: 1

Arun Shankar
Arun Shankar

Reputation: 2593

You don't need to use that constructor. You can assign values to variables if the variable name are same as the firebase node values. Try this:

database.limitToLast(1).addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Curp value=dataSnapshot.getValue(Curp.class);
                    //Use this object
                }
            });

Upvotes: 1

Related Questions