java.lang.IndexOutOfBoundsException:索引:1,大小:1位于java.util.ArrayList.get(ArrayList.java:437)
我正在尝试将我的食品订单列表中的项目添加到recyclerView中,但我总是遇到一个例外。 以下是我的请求模型类代码:java.lang.IndexOutOfBoundsException:索引:1,大小:1位于java.util.ArrayList.get(ArrayList.java:437),java,android,android-studio,Java,Android,Android Studio,我正在尝试将我的食品订单列表中的项目添加到recyclerView中,但我总是遇到一个例外。 以下是我的请求模型类代码: private void loadOrders(String phone) { adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder> ( Request.class, R.
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);
}
Requests.java
在populateViewHolder中,我们将所有数据放入recyclerView
除了订单食品列表中的这两行:
orderViewHolder.txtProductName.setText(request.getFoods().get(i).getProductName());
orderViewHolder.txtPrice.setText(request.getFoods().get(i).getPrice());
这里我们使用get()函数从模型类中获取产品名称。。。
以下是我的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);
}
private void loadOrders(字符串电话)
{
适配器=新的FirebaseRecyclerAdapter
(
请求类,
R.layout.order_布局,
OrderViewHolder.class,
请求。orderByChild(“电话”)
.equalTo(电话)
)
{
@凌驾
受保护的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());//这里这两行向我显示了一条错误消息
orderViewHolder.txtPrice.setText(request.getFoods().get(i.getPrice());//这里这两行向我显示了一条错误消息
orderViewHolder.txtPersonname.setText(request.getName());
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(适配器);
}
以下是我的订单布局代码:
order_layout.xml
错误在这一行
orderViewHolder.txtProductName.setText(request.getFoods().get(i.getProductName());
“i”是recyclerview中的视图位置或项目位置
看起来你有更多的请求项,然后列出食物包含的内容那么请告诉我如何解决。。。。
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);
}
<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>