gpt4 book ai didi

java - DatabaseException : Can't convert object of type java. lang.String 类型 Models.OrderDetails

转载 作者:行者123 更新时间:2023-12-01 16:18:24 25 4
gpt4 key购买 nike

enter image description here

这是我的数据库结构。我正在尝试获取订单详细信息。

这是代码

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

但我收到此错误

Process: com.mycompany.grocerystore, PID: 20464 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.mycompany.grocerystore.Models.OrderDetails at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.3.0:435) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.3.0:231) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.3.0:79) at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.3.0:203) at com.mycompany.grocerystore.Fragments.PreviousOrder$2.onDataChange(PreviousOrder.java:77) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0:55) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

这是我收到错误的行

OrderDetails details = grandchild.getValue(OrderDetails.class);

如果我从 for (DataSnapshot grandchild : childSnapshot.child("Order Details").getChildren()) 中删除 .child(Order Details)。该代码有效,但它还显示 Products 的子项,所有值均为 null。这是一个屏幕截图。 enter image description here

有什么帮助或建议吗?

最佳答案

问题是您正在尝试将字符串(OrderDetails 中的值)转换为 OrderDetails 对象

解决方案:

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

关于java - DatabaseException : Can't convert object of type java. lang.String 类型 Models.OrderDetails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62340676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com