0

i'm trying to inflate a CardView with information taken from FireBase. My problem is that the reference to the database read it but the RecyclerView miss to get it... I'm new on Android, i'll aprecite so much your help.

This is my Java Class:

public class ResumenActivity extends AppCompatActivity {

DatabaseReference refMV;
RecyclerView recyclerViewResumen;
ArrayList<Listado> listaResumen;
AdaptadorResumen adaptadorResumen;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resumen);

    recyclerViewResumen = (RecyclerView) findViewById(R.id.listadoRecyclerView);
    recyclerViewResumen.setLayoutManager(new LinearLayoutManager(this));

    refMV.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            listaResumen = new ArrayList<Listado>();

            Listado l = dataSnapshot.getValue(Listado.class);
            listaResumen.add(l);

            adaptadorResumen = new AdaptadorResumen(ResumenActivity.this, listaResumen);
            recyclerViewResumen.setAdapter(adaptadorResumen);
            recyclerViewResumen.setHasFixedSize(true);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(ResumenActivity.this, "Upss.. Algo anda mal!", Toast.LENGTH_SHORT).show();
        }
    });
    }
}

This is the Model:

package app.technologias8.smartbarprototipo.modelos;

public class Listado {

private String Nombre;
private String Precio;

public Listado() {
}

public Listado(String nombre, String precio) {
    this.Nombre = nombre;
    this.Precio = precio;
}

public String getNombre() {
    return this.Nombre;
}

public void setNombre(String nombre) {
    this.Nombre = nombre;
}

public String getPrecio() {
    return this.Precio;
}

public void setPrecio(String precio) {
    this.Precio = precio;
}
}

The Firebase:

Firebase Database

Playa
  • 23
  • 4

2 Answers2

0

You need to add just a layout manager to your Recycleview like this

refMV.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            listaResumen = new ArrayList<Listado>();

            Listado l = dataSnapshot.getValue(Listado.class);
            listaResumen.add(l);

            adaptadorResumen = new AdaptadorResumen(ResumenActivity.this, listaResumen);
            recyclerViewResumen.setLayoutManager(ResumenActivity.this);//ADD THIS
            recyclerViewResumen.setAdapter(adaptadorResumen);
            recyclerViewResumen.setHasFixedSize(true);
        }
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
0

The RecyclerView needs an adapter, even an empty one, in order to perform its layout properly. Since your database call is happening asynchronously, the AdaptadorResumen that it creates in onDataChange is not ready in time for android to do its initial layout, so the whole thing is ignored.

Try setting a null adaptor in onCreate. It will be replaced when your database call is complete.

recyclerViewResumen = (RecyclerView) findViewById(R.id.listadoRecyclerView);
recyclerViewResumen.setLayoutManager(new LinearLayoutManager(this));
adaptadorResumen = new AdaptadorResumen(ResumenActivity.this, null);
recyclerViewResumen.setAdapter(adaptadorResumen);
tobiasfried
  • 1,689
  • 8
  • 19