本文介绍了DatabaseException:无法将类型为java.lang.String的对象转换为Models.OrderDetails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数据库结构.我正在尝试获取订单详细信息.
This is my database structure. I am trying to fetch Order Details.
这是代码
databaseReference = FirebaseDatabase.getInstance().getReference().child("Admin Order").
child(userID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()){
for (DataSnapshot grandchild : childSnapshot.child("Order Details").getChildren()){
OrderDetails details = grandchild.getValue(OrderDetails.class);
mList.add(details);
progressBar.setVisibility(View.INVISIBLE);
}
}
adapter = new PreviousOrderAdapter(getContext(), mList);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressBar.setVisibility(View.INVISIBLE);
}
});
但是我收到此错误
这是我收到错误的行
OrderDetails details = grandchild.getValue(OrderDetails.class);
如果我从(DataSnapshot孙子:childSnapshot.child(订单详细信息").getChildren())的中删除
.child(订单详细信息)
.该代码有效,但它还会显示 Products
的子代,其所有值均为null.这是屏幕截图.
If I remove .child(Order Details)
from for (DataSnapshot grandchild : childSnapshot.child("Order Details").getChildren())
. The Code works but it also show the child of Products
with all value as null. Here is a Screenshot.
有什么帮助或建议吗?
推荐答案
问题是您正在尝试将字符串( OrderDetails
中的值)转换为 OrderDetails
对象
The problem is you are trying to convert a string ( the values in OrderDetails
) to OrderDetails
object
解决方案:
databaseReference = FirebaseDatabase.getInstance().getReference().child("Admin Order").
child(userID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
OrderDetails orderDetails = childSnapshot.child("Order Details").getValue(OrderDetails.class);
Log.w("orderDetails", orderDetails.getConsumer());
mList.add(details);
progressBar.setVisibility(View.INVISIBLE);
}
adapter = new PreviousOrderAdapter(getContext(), mList);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
progressBar.setVisibility(View.INVISIBLE);
}
});
这篇关于DatabaseException:无法将类型为java.lang.String的对象转换为Models.OrderDetails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!