Home > Software engineering >  android - how do you display an array that is stored in firestore
android - how do you display an array that is stored in firestore

Time:03-20

I am trying to display an array that I stored in my Firestore to be displayed in a RecyclerView.

the error I'm receiving is this:

java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.util.ArrayList to String

As for my codes, here it is:

model

public class ViewOrderModel implements Serializable {

    String userName;
    String phoneNumber;
    String currentTime;
    String address;
    long totalPrice;
    String itemList;
    String documentID;
    String productName;
    int totalQuantity;

adapter


    @Override
    public void onBindViewHolder(@NonNull ViewOrderAdapter.ViewHolder holder, int position) {

        holder.userName.setText(viewOrderModelList.get(position).getUserName());
        holder.date.setText(viewOrderModelList.get(position).getCurrentTime());
        holder.totalPrice.setText(String.valueOf(viewOrderModelList.get(position).getTotalPrice()));
        holder.address.setText(viewOrderModelList.get(position).getAddress());
        holder.phone.setText(viewOrderModelList.get(position).getPhoneNumber());

        ViewOrderModel viewOrderModel = viewOrderModelList.get(position);
        holder.productName.setText(viewOrderModel.getProductName());
        holder.quantity.setText(viewOrderModel.getTotalQuantity());

    }

activity

    order_rec = findViewById(R.id.admin_view_order_rec);
        order_rec.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        viewOrderModelList = new ArrayList<>();
        viewOrderAdapter = new ViewOrderAdapter(this,viewOrderModelList);
        order_rec.setAdapter(viewOrderAdapter);
        firestore.collection("UserOrder")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()){
                            for (QueryDocumentSnapshot documentSnapshot : task.getResult()){
                                ViewOrderModel viewOrderModel = documentSnapshot.toObject(ViewOrderModel.class);
                                viewOrderModelList.add(viewOrderModel);
                                viewOrderAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                });

if it's needed this is my database

enter image description here

CodePudding user response:

You are getting the following error:

java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type java.util.ArrayList to String

Because you are trying to read a property of type array as a String, which is not possible in Java. To solve this, you have to change the type of your itemList filed in your ViewOrderModel class, to be a list. So please change:

String itemList;

Into:

List<Product> itemList;

This will work only as long as you have a class with a minimum declaration that looks like this:

class Product {
    String documentID, productName;
    int productPrice, totalPrice, totalQuantity;
}
  • Related