У меня есть 2 Activity, MainActivity и ProductDetailsActivity. В MainActivity я показываю 3 макета в RecyclerView, таких как (Feature, 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)