Good afternoon, I have a problem when populating recyclerview with data from Firebase database.
These are the data that I have stored in the Firebase database:
They all have the same characteristics (description, image, price, type).
My problem is when I want to populate ALL EXISTING DATA ( from Tombstones to Mugs ) in the RecyclerView, it does not show me anything, I do not have an error in the custom RecyclerView or nothing, because when populating with the data of, for example, "tiles", this works perfectly for me, I leave here the code where the recyclerview is filled. I'm probably doing something wrong here.
public class ProductosActivity extends AppCompatActivity {
ProgressDialog progressDialog;
private RecyclerView recyclerProductos;
FirebaseDatabase firebaseDatabase;
DatabaseReference data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_productos);
recyclerProductos = (RecyclerView) findViewById(R.id.recyclerProductos);
progressDialog = new ProgressDialog(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerProductos.setLayoutManager(linearLayoutManager);
progressDialog.setMessage("Cargando imagenes..");
progressDialog.show();
firebaseDatabase = FirebaseDatabase.getInstance();
data = firebaseDatabase.getReference("productos");
data.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<ProductoBean> pb = new ArrayList<ProductoBean>();
pb.clear();
for (DataSnapshot dt : dataSnapshot.getChildren()) {
ProductoBean prod = dt.getValue(ProductoBean.class);
Log.i("Datos", dt.getValue().toString());
pb.add(prod);
}
ProductoAdaptador adaptador = new ProductoAdaptador(ProductosActivity.this, pb);
progressDialog.dismiss();
recyclerProductos.setAdapter(adaptador);
adaptador.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Error", databaseError.getMessage());
}
});
}
}
When I want to only populate data from for example "tiles", I do this and it works correctly.
.
.
.
data.child("lozas")addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<ProductoBean> pb = new ArrayList<ProductoBean>();
pb.clear();
for (DataSnapshot dt : dataSnapshot.getChildren()) {
ProductoBean prod = dt.getValue(ProductoBean.class);
Log.i("Datos", dt.getValue().toString());
pb.add(prod);
}
ProductoAdaptador adaptador = new ProductoAdaptador(ProductosActivity.this, pb);
progressDialog.dismiss();
recyclerProductos.setAdapter(adaptador);
adaptador.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Error", databaseError.getMessage());
}
});
}
}
UPDATE This code is from the recyclerview adapter.
public class ProductoAdaptador extends RecyclerView.Adapter<ProductoAdaptador.ProductoViewHolder> {
ArrayList<ProductoBean> productoBean;
Context context;
public ProductoAdaptador(Context ctx, ArrayList<ProductoBean> pb){
this.context = ctx;
this.productoBean = pb;
}
@Override
public ProductoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recyclerview_productos,parent,false);
ProductoViewHolder holder = new ProductoViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ProductoViewHolder holder, int position) {
ProductoBean prod = productoBean.get(position);
Picasso.with(context).load(prod.getImagen()).into(holder.imagen);
//holder.imagen.setImageUrl(prod.getImagen());
holder.tipo.setText(prod.getTipo());
holder.descripcion.setText(prod.getDescripcion());
holder.precio.setText(prod.getPrecio());
}
@Override
public int getItemCount() {
return productoBean.size();
}
public static class ProductoViewHolder extends RecyclerView.ViewHolder {
SmartImageView imagen;
AutofitTextView tipo, descripcion, precio;
public ProductoViewHolder(View itemView) {
super(itemView);
imagen = (SmartImageView) itemView.findViewById(R.id.SmartImageProducto);
tipo = (AutofitTextView) itemView.findViewById(R.id.txtTipoProducto);
descripcion = (AutofitTextView) itemView.findViewById(R.id.txtDescripcionProducto);
precio = (AutofitTextView) itemView.findViewById(R.id.txtPrecioProducto);
}
}
}
UPDATE 2
Bean Product Class (get / set / constructor)
public class ProductoBean {
private String descripcion;
private String imagen;
private String precio;
private String tipo;
public ProductoBean(){
}
public ProductoBean(String descripcion, String imagen, String precio, String tipo){
this.descripcion = descripcion;
this.imagen = imagen;
this.precio = precio;
this.tipo = tipo;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getImagen() {
return imagen;
}
public void setImagen(String imagen) {
this.imagen = imagen;
}
public String getPrecio() {
return precio;
}
public void setPrecio(String precio) {
this.precio = precio;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
}
Then finally my question is how to populate the recycler with all the data (from tombstones to cups).