RecyclerView:未连接适配器;跳过布局CardView

时间:2019-04-11 04:45:16

标签: android firebase recycler-adapter android-cardview

我正在尝试使用从CardView获取的信息来对FireBase进行膨胀。我的问题是对数据库的引用读取了它,但是RecyclerView却错过了它...我是Android上的新手,我会非常感谢您的帮助。

这是我的Java类:

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();
        }
    });
    }
}

这是模型:

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;
}
}

Firebase:

Firebase Database

2 个答案:

答案 0 :(得分:0)

您只需像这样向您的Recycleview添加布局管理器

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);
        }

答案 1 :(得分:0)

RecyclerView需要一个适配器,甚至是一个空适配器,以正确执行其布局。由于您的数据库调用是异步发生的,因此它在AdaptadorResumen中创建的onDataChange尚未及时准备好让android进行初始布局,因此整个操作都将被忽略。

尝试在onCreate中设置一个空适配器。您的数据库调用完成后,它将被替换。

recyclerViewResumen = (RecyclerView) findViewById(R.id.listadoRecyclerView);
recyclerViewResumen.setLayoutManager(new LinearLayoutManager(this));
adaptadorResumen = new AdaptadorResumen(ResumenActivity.this, null);
recyclerViewResumen.setAdapter(adaptadorResumen);