Reputation: 23
I'm trying to inflate a cardView set on a RecyclerView, taking data from FireBase. My problem is:
DatabaseException: Can't convert object of type java.lang.Long to type app.technologias8.smartbarprototipo.modelos.Pedido
So they tell me that the problem is on
Pedido p = dataSnapshot1.getValue(Pedido.class);
My Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.articulos_seleccionados);
refMesaVirtual.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listaPedido = new ArrayList<Pedido>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Pedido p = dataSnapshot1.getValue(Pedido.class); //!!
listaPedido.add(p);
}
adaptadorListarPedidos = new AdaptadorListarPedidos(ArticulosSeleccionadosActivity.this, listaPedido/*, nombre, precio*/);
recyclerViewPedidos.setAdapter(adaptadorListarPedidos);
recyclerViewPedidos.setHasFixedSize(true);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ArticulosSeleccionadosActivity.this, "Upss.. Algo anda mal!", Toast.LENGTH_SHORT).show();
}
});
}
}
My Model
public class Pedido {
private String Nombre;
private String Precio;
public Pedido() {
}
public Pedido(String nombre, String precio) {
Nombre = nombre;
Precio = precio;
}
public String getNombre() {
return Nombre;
}
public void setNombre(String nombre) {
this.Nombre = nombre;
}
public String getPrecio() {
return Precio;
}
public void setPrecio(String precio) {
this.Precio = precio;
}
}
And my DataBase
![enter image description here][1]
[1]: https://i.sstatic.net/Nmt0l.png
Upvotes: 1
Views: 666
Reputation: 80924
Since you are using your own custom class, then there is no need to iterate to retrieve the data. When you are iterating, you are retrieving some data of type Long
therefore you get that error. To solve your problem, you need to remove the for loop:
refMesaVirtual.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listaPedido = new ArrayList<Pedido>();
Pedido p = dataSnapshot.getValue(Pedido.class); //!!
listaPedido.add(p);
adaptadorListarPedidos = new AdaptadorListarPedidos(ArticulosSeleccionadosActivity.this, listaPedido/*, nombre, precio*/);
recyclerViewPedidos.setAdapter(adaptadorListarPedidos);
recyclerViewPedidos.setHasFixedSize(true);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ArticulosSeleccionadosActivity.this, "Upss.. Algo anda mal!", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1