Java 如何在adapter recyclerview中使用两个arraylist
我想在同一适配器中使用两个arraylist,但出现了错误。它没有设置值,我得到的是索引超出范围的期望值 这是我的适配器代码:Java 如何在adapter recyclerview中使用两个arraylist,java,android,android-recyclerview,android-adapter,Java,Android,Android Recyclerview,Android Adapter,我想在同一适配器中使用两个arraylist,但出现了错误。它没有设置值,我得到的是索引超出范围的期望值 这是我的适配器代码: public class packagingProductAdapter extends RecyclerView.Adapter<packagingProductAdapter.ViewHolder> { private List<packingProductPOJO> list_data; // private List<
public class packagingProductAdapter extends RecyclerView.Adapter<packagingProductAdapter.ViewHolder> {
private List<packingProductPOJO> list_data;
// private List<packingProductPOJO> testPOJO;
private List<testPOJO> test_POJO;
// private packingProduct context;
private testClass context;
String product_id, category_id, quantity = "0", sizeValue = "0";
private static int _value = 0;
PrefManager prefManager;
ProgressLoader progressLoader;
/*
public packagingProductAdapter(List<packingProductPOJO> list_data, List<testPOJO> testPOJO, packingProduct context) {
this.list_data = list_data;
this.test_POJO = testPOJO;
this.context = context;
prefManager = new PrefManager(context);
progressLoader = new ProgressLoader(context);
} */
public packagingProductAdapter(List<packingProductPOJO> list_data, List<testPOJO> test_POJO, testClass context) {
this.list_data = list_data;
this.test_POJO = test_POJO;
this.context = context;
prefManager = new PrefManager(context);
progressLoader = new ProgressLoader(context);
}
@Override
public packagingProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.packing_product_cart_view_row,parent,false);
return new packagingProductAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
final packingProductPOJO listData = list_data.get(position);
final testPOJO test_POJO1 = test_POJO.get(position);
holder.setIsRecyclable(false);
if (test_POJO.equals("null")){
holder.linearLayout.setVisibility(View.GONE);
}else
{
holder.linearLayout.setVisibility(View.VISIBLE);
holder.heading.setText(test_POJO1.getContact_name());
// holder.linearLayout.setBackgroundResource((R.drawable.rectangleborder));
}
holder.produt_quantity.setText("= " +listData.getQuantity() );
if (listData.getProductPdf().equals("null")) {
holder.product_name.setText(listData.getCat_name());
}else {
holder.product_name.setText("DOWNLOAD PDF");
}
// holder.heading.setText(listData.getImage());
product_id = listData.getProductid();
category_id = listData.getImage();
holder.purchaseQty.setText(listData.getUser_quantity());
holder.pro_price.setText("Rs. " +listData.getPrice());
final String finalPro_qty = "0";
final String finalProduct_ID = product_id;
holder.count_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String removeProductId = listData.getId();
String removeQuantity = listData.getUser_quantity();
int quantity = 0;
try{
quantity = Integer.parseInt(removeQuantity) + 1;
} catch(NumberFormatException ex){ // handle your exception
ex.printStackTrace();
}
if (quantity > Integer.parseInt(listData.getQuantity())){
Toast.makeText(context, "Insufficent Stock ", Toast.LENGTH_LONG).show();
}else {
// context.add("pk@gmail.com", String.valueOf(quantity), finalProduct_ID, "manual", category_id, holder.purchaseQty, position, "0");
}
}
});
holder.count_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String removeProductId = listData.getId();
String removeQuantity = listData.getUser_quantity();
final int quantity = Integer.valueOf(removeQuantity) - 1;
// context.add("pk@gmail.com", String.valueOf(quantity), finalProduct_ID, "manual", category_id, holder.purchaseQty, position, "0");
}
});
holder.purchaseQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get alert_dialog.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.alert_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set alert_dialog.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userSize = (EditText) promptsView.findViewById(R.id.etUserInput);
final TextView enterQuantity = promptsView.findViewById(R.id.enterQuantity);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Toast.makeText(context, "Entered: "+userInput.getText().toString(), Toast.LENGTH_LONG).show();
final String quantityValue = (userSize.getText().toString());
if (TextUtils.isEmpty(quantityValue)) {
userSize.setError("Quantity cannot be Empty");
Toast.makeText(context, "Quantity cannot be Empty", Toast.LENGTH_LONG).show();
userSize.requestFocus();
return;
}
final int quantity = Integer.valueOf(quantityValue);
if (quantity > Integer.parseInt(listData.getQuantity())){
Toast.makeText(context, "Insufficent Stock ", Toast.LENGTH_LONG).show();
}else {
// context.add("pk@gmail.com", String.valueOf(quantity), finalProduct_ID, "manual", category_id, holder.purchaseQty, position, sizeValue);
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
@Override
public int getItemCount() {
return list_data.size() + test_POJO.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CardView cardView;
private TextView purchaseQty, product_name, produt_quantity, heading, pro_price;
private ImageView count_up, count_down;
private LinearLayout topview, userQuantityLayout;
private LinearLayout linearLayout;
public ViewHolder(View itemView) {
super(itemView);
heading = itemView.findViewById(R.id.heading);
purchaseQty= itemView.findViewById(R.id.txtItemQty);
produt_quantity= itemView.findViewById(R.id.total_qty);
product_name=itemView.findViewById(R.id.pro_name);
purchaseQty.setText("0");
count_up = itemView.findViewById(R.id.count_up);
count_down = itemView.findViewById(R.id.count_down);
pro_price = itemView.findViewById(R.id.pro_price);
cardView = itemView.findViewById(R.id.packing_card_view);
cardView.setVisibility(View.VISIBLE);
// purchaseQty.setBackground(context.getResources().getDrawable(R.drawable.rectangleborder));
topview = itemView.findViewById(R.id.topview);
topview.setVisibility(View.GONE);
linearLayout = itemView.findViewById(R.id.linearheading);
}
}
}
这里我添加了logcat。我得到的结果就在那里。这是因为以下代码:
@Override
public int getItemCount() {
return list_data.size() + test_POJO.size();
}
您将返回两个列表的总和:
例如,假设您正在返回:return3+3//total=6
但您也尝试访问以下列表中的个人列表:
@Override
public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
final packingProductPOJO listData = list_data.get(position);
final testPOJO test_POJO1 = test_POJO.get(position);
当您在列表上滚动6次时,位置将增加,这是两个列表的总和getItemCount()
,它将滚动java.lang.IndexOutOfBoundsException:Index:3,Size:3
尝试仅返回一个列表的大小:
@Override
public int getItemCount() {
return list_data.size();
}
getItemCount()方法中的代码是
@Override
public int getItemCount() {
return list_data.size() + test_POJO.size();
}
您将返回2个不同数组的大小。onBindViewHolder()方法具有位置变量。此变量提供项目位置,此位置的范围为0..getItemCount()
在onBindViewHolder()方法中有以下代码
final packingProductPOJO listData = list_data.get(position);
final testPOJO test_POJO1 = test_POJO.get(position);
getItemCount()方法返回两个不同数组的大小,但您尝试访问相同的位置。这是例外的原因。如果数组大小相同,则可以使用
@Override
public int getItemCount() {
return list_data.size();
}
但如果没有,您可以这样做:
@Override
public int getItemCount() {
return Math.max(list_data.size(), test_POJO.size());
}
在onBindViewHolderMethod()中
但是这不是一个好方法,试试这个
@Override
public int getItemCount() {
return min(list_data.size(), test_POJO.size());
}
它将解决当前的问题。但是您会发现
RecyclerView
中缺少一些数据。如果列表中包含类似的元素,您可以将它们组合起来,然后在适配器中作为一个列表使用,这样做可以避免各种麻烦
List<PojoClass> combined = new ArrayList<>();
combined.addAll(firstList);
combined.addAll(secondList);
List combined=new ArrayList();
合并。添加全部(第一个列表);
合并。添加全部(第二个列表);
您已在项目计数中返回了list\u data.size()+test\u POJO.size()
。这意味着如果list\u data
包含2项,而test\u POJO
包含3项,则您的recyclerview将显示5项onBindViewHolder
将调用5次。根据您的代码,您需要同时从list\u data
和test\u POJO
中获取数据。那么如何确保这两个列表具有相同大小的数据呢。是否可能test\u POJO
的数据少于list\u data
?是的,我确认test\u POJO的项目少于list\u data是他们的任何解决方案,我可以同时拥有两个数组列表数据是的,有解决方案。但是我需要知道一些信息。如果test\u POJO
中没有数据,那么您将怎么做?test\u pojob中肯定有数据,但是它不会从test\u POJO数组列表返回数据,我也尝试过。
@Override
public int getItemCount() {
return min(list_data.size(), test_POJO.size());
}
List<PojoClass> combined = new ArrayList<>();
combined.addAll(firstList);
combined.addAll(secondList);