Home > database >  android - how to display the array of object from Cloud FireStore in the RecyclerView
android - how to display the array of object from Cloud FireStore in the RecyclerView

Time:03-23

I'm trying to display the data that I stored in the Cloud Firestore to a RecyclerView but when I run the application it displays all except the itemList only the map is being displayed

I don't receive any error so it leads me to a bottleneck

enter image description here

enter image description here

the code for my adapter


    Context context;
    List<ViewOrderModel> viewOrderModelList;

    public ViewOrderAdapter(Context context, List<ViewOrderModel> viewOrderModelList) {
        this.context = context;
        this.viewOrderModelList = viewOrderModelList;
    }

   @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(String.valueOf(viewOrderModel.getTotalQuantity()));

    }

Model Class

public class ViewOrderModel implements Serializable {

    String userName,phoneNumber,currentTime,address,documentID,productName;
    long totalPrice;
    List<ViewOrderModel> itemList;
    int totalQuantity;

Main 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()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        if (!queryDocumentSnapshots.isEmpty()){
                            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
                            for (DocumentSnapshot docSnap : list) {
                                ViewOrderModel orderModel = docSnap.toObject(ViewOrderModel.class);
                                viewOrderModelList.add(orderModel);
                            }
                            viewOrderAdapter.notifyDataSetChanged();
                        }
                    }
                });

Is there a way to display the data or any other way different for what I'm doing in my codes

CodePudding user response:

You need to two different classes. The first one would be, for example, ViewOrderModel class, which will hold the fields as address, currentTime, documentID, and so on.

And the second one called Product:

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

Which will hold the properties corresponding to the object that exists in the itemList. So your ViewOrderModel class should look like this:

public class ViewOrderModel implements Serializable {
    String userName,phoneNumber,currentTime,address,documentID,productName;
    List<Product> itemList;
                       
  • Related