-3

I am trying to add the items from my List food order into the recyclerView but i always get an exception. Below is my Requests Model class code:

Requests.java

public class Request {
    private String phone;
    private String name;
    private String address;
    private String total;
    private String status;
    private List<Order> foods;    // list of food order

    public Request() {
    }

    public Request(String phone, String name, String address, String total, List<Order> foods) {
        this.phone = phone;
        this.name = name;
        this.address = address;
        this.total = total;
        this.foods = foods;
        this.status = "0";    //Default is 0,0: Placed, 1:Shipping,2: Shipped
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public List<Order> getFoods() {
        return foods;
    }

    public void setFoods(List<Order> foods) {
        this.foods = foods;
    }
}

I will get all the items easily which are not containing in the List into the recyclerView such as name,phone,address... but could not get items from List order food. Below is my OrderViewHolder class code:

OrderViewHolder.java

public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{

    public TextView txtOrderId,txtOrderStatus,txtOrderPhone,txtOrderAddress,txtProductName,txtPrice,txtPersonname;

    private ItemClickListener itemClickListener;

    public OrderViewHolder(@NonNull View itemView)
    {
        super(itemView);
        txtOrderAddress = (TextView)itemView.findViewById(R.id.order_address);
        txtOrderId = (TextView)itemView.findViewById(R.id.order_id);
        txtOrderStatus = (TextView)itemView.findViewById(R.id.order_status);
        txtOrderPhone = (TextView)itemView.findViewById(R.id.order_phone);
        txtProductName=(TextView)itemView.findViewById(R.id.order_productname);
        txtPrice=(TextView)itemView.findViewById(R.id.order_price);
        txtPersonname= (TextView)itemView.findViewById(R.id.order_name);

        itemView.setOnClickListener(this);
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View v)
    {
        itemClickListener.onClick(v,getAdapterPosition(),false);
    }

}

In populateViewHolder we gets all data into the recyclerView except these two lines which are in List of Order food:

orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName());
orderViewHolder.txtPrice.setText(request.getFoods().get(i).getPrice());

Here we use get() function to get our Product Name from the Model class... Below is my populateViewHolder class code:

private void loadOrders(String phone)
    {
        adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>
                (
                Request.class,
                R.layout.order_layout,
                OrderViewHolder.class,
                requests.orderByChild("phone")
                        .equalTo(phone)
                )
        {
            @Override
            protected void populateViewHolder(OrderViewHolder orderViewHolder, Request request, int i)
            {
               orderViewHolder.txtOrderId.setText(adapter.getRef(i).getKey());
               orderViewHolder.txtOrderStatus.setText(convertCodeToStatus(request.getStatus()));
               orderViewHolder.txtOrderAddress.setText(request.getAddress());
               orderViewHolder.txtOrderPhone.setText(request.getPhone());
               orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName());     //Here these two line shows up an error message to me
               orderViewHolder.txtPrice.setText(request.getFoods().get(i).getPrice());                //Here these two line shows up an error message to me
               orderViewHolder.txtPersonname.setText(request.getName());
            }
        };
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);
    }

Below is my order_layout code:

order_layout.xml

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardElevation="4dp">

    <LinearLayout
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="9"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/order_id"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="#111111"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/order_status"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="Status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/order_phone"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="123456789"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/order_name"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="Person Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


            <TextView
                android:id="@+id/order_address"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="Address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/order_productname"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="Product Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/order_price"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="italic"
                android:text="Price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>

Here is my database looks like: enter image description here

1 Answers1

-1

The error is in this line orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName()); That "i" its a position of view or position of item in recyclerview And looks like u have more request items then list foods containes