简体   繁体   中英

DatabaseException: Can't convert object of type java.lang.String to type Models.OrderDetails

在此处输入图像描述

This is my database structure. I am trying to fetch Order Details.

Here is the code

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

But I am getting this Error

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.ZBF12E1515C25C7D 8C0352F1413AB9A15Z: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 ZC31B32364CE19CA8FCD1 50A417ECCE58Z.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)

This is the Line I am getting the error

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

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. 在此处输入图像描述

Any help or suggestions?

The problem is you are trying to convert a string ( the values in OrderDetails ) to OrderDetails object

Solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM