我正在尝试将我的 List 食品订单中的项目添加到 recyclerView 中,但我总是遇到异常。 下面是我的请求模型类代码:
<块引用>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;
}
}
我将轻松地将列表中未包含的所有项目(例如姓名、电话、地址)放入 recyclerView 中...但无法从 List 订单食品中获取项目。 下面是我的 OrderViewHolder 类代码:
<块引用>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);
}
}
在 populateViewHolder 中,我们将所有数据放入 recyclerView 除了订单食品列表中的这两行:
orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName());
orderViewHolder.txtPrice.setText(request.getFoods().get(i).getPrice());
这里我们使用 get() 函数从 Model 类中获取我们的产品名称... 下面是我的 populateViewHolder 类代码:
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);
}
以下是我的 order_layout 代码:
<块引用>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>
这是我的数据库: enter image description here
答案 0 :(得分:-1)
错误在这一行 orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName()); 那个“我”它是一个视图位置或项目在回收站视图中的位置 看起来你有更多的请求项目然后列出食物包含