我有2个活动,MainActivity和ProductDetailsActivity。在MainActivity中,我在RecyclerView中显示3个布局,例如(功能,Bioderma和Supplement),在ProductDetailsActivity中,我显示另一个RecyclerView的产品详细信息,例如(相关产品)。相关产品RecyclerView显示完美的数据,但是当我单击MainActivity第一个产品时,没有问题,并且在单击第二个产品后,我的应用程序显示IndexOutOfBoundsException。
我是编程新手,我尝试了许多不同的方法,但是没有运气。请帮助我。
我的ProductAdapter.java如下:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private Context mContext;
private ArrayList<Product> productList, relatedProductList;
private int viewType;
public ProductAdapter(Context mContext, ArrayList<Product> productList, ArrayList<Product> relatedProductList, int viewType) {
this.mContext = mContext;
this.productList = productList;
this.relatedProductList = relatedProductList;
this.viewType = viewType;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == 1) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_product_list, parent, false);
} else if (viewType == 2) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.circular_horizontal_product_list, parent, false);
} else if (viewType == 4) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_related_product_list, parent, false);
} else
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_list, parent, false);
return new ViewHolder(view);
}
@Override
public int getItemViewType(int position) {
if (viewType == 1) {
return 1;
} else if (viewType == 2) {
return 2;
} else if (viewType == 4) {
return 4;
} else
return 3;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (viewType == 4) {
holder.productTitle.setText(relatedProductList.get(position).getProductName());
String imagePath = relatedProductList.get(position).getProductImage();
Glide.with(mContext)
.load(imagePath)
.placeholder(R.drawable.loading)
.into(holder.productImage);
} else {
holder.productTitle.setText(productList.get(position).getProductName());
//Converting string resource with placeholder
String salePrice = holder.itemView.getContext().getString(R.string.sale_price, Float.parseFloat(productList.get(position).getSalePrice()));
String regularPrice = holder.itemView.getContext().getString(R.string.regular_price, Float.parseFloat(productList.get(position).getSalePrice()));
//Checking for equal price and invisible regular text view
if (salePrice.equals(regularPrice)) {
holder.productSalePrice.setText(salePrice);
} else {
holder.productSalePrice.setText(salePrice);
holder.productRegularPrice.setText(regularPrice);
holder.productRegularPrice.setPaintFlags(holder.productRegularPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
String imagePath = productList.get(position).getProductImage();
Glide.with(mContext)
.load(imagePath)
.placeholder(R.drawable.loading)
.into(holder.productImage);
}
}
@Override
public int getItemCount() {
if (viewType == 4) {
return relatedProductList == null ? 0 : relatedProductList.size();
} else
return productList == null ? 0 : productList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
MaterialTextView productTitle, productRegularPrice, productSalePrice;
ImageView productImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.ivProductImageId);
productTitle = itemView.findViewById(R.id.tvTitleId);
productRegularPrice = itemView.findViewById(R.id.tvRegularPriceId);
productSalePrice = itemView.findViewById(R.id.tvSalePriceId);
//for passing data between activities
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewType == 4) {
final int relatedPosition = getAdapterPosition();
if (relatedPosition != RecyclerView.NO_POSITION) {
Product relatedSelectProduct = relatedProductList.get(relatedPosition);
Intent intent = new Intent(mContext, ProductDetailsActivity.class);
intent.putExtra("productId", relatedSelectProduct.getProductId());
mContext.startActivity(intent);
}
} else {
final int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Product selectedProduct = productList.get(position);
Intent intent = new Intent(mContext, ProductDetailsActivity.class);
intent.putExtra("productId", selectedProduct.getProductId());
mContext.startActivity(intent);
}
}
}
});
}
}
}
我的IDE指向此粗体代码行:
else {
final int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
**Product selectedProduct = productList.get(position);**
Intent intent = new Intent(mContext, ProductDetailsActivity.class);
intent.putExtra("productId", selectedProduct.getProductId());
mContext.startActivity(intent);
}
我的logcat显示此错误:
java.lang.IndexOutOfBoundsException: Index: 6, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.themessenger.epharma.adapter.ProductAdapter$ViewHolder$1.onClick(ProductAdapter.java:146)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
从您的堆栈跟踪中,productList
的大小为0。因此,当您获得适配器位置(例如1,2,3等)并尝试从列表中获取项目时,它将抛出IndexOutOfBoundsException
。建议您调查列表数据,并确保正确填充了列表。另外,如果在同一适配器中有两个项目计数为4的列表,则适配器位置可能为7,所以当您尝试从一个列表中获取项目时,您仍然会遇到相同的异常,因为没有一个列表具有4个以上的项目。也许尝试将列表合并为单个列表,我认为它将解决您的许多问题。
我找到了解决方案。我在MainActivity中使用3个RecyclerView,在ProductDetailsActivity中使用1个RecyclerView。当我从MainActivity移到ProductDetailsActivity时,会清除MainActivity产品列表数组的大小,因此,当我通过onBackPresssed()返回MainActivity时,我的产品列表大小设置为0。
[我做了什么,在MainActivity中,我重写了onRestart()生命周期方法,并通过设置Volley数据获取方法从Web上获取了数据。我不知道会有什么后果,但它挽救了我的生命。
感谢大家的支持。