如何在适配器的RecyclerView中使用两个ArrayList。

huangapple go评论98阅读模式
英文:

How to use two arraylist in adapter recyclerview

问题

  1. I want to use two arraylist in same adapter but I'm getting error. It is not setting the value and I'm getting INDEX OUT BOUND EXCEPTION
  2. This is my adapter code:
  3. public class packagingProductAdapter extends RecyclerView.Adapter<packagingProductAdapter.ViewHolder> {
  4. private List<packingProductPOJO> list_data;
  5. // private List<packingProductPOJO> testPOJO;
  6. private List<testPOJO> test_POJO;
  7. // private packingProduct context;
  8. private testClass context;
  9. String product_id, category_id, quantity = "0", sizeValue = "0";
  10. private static int _value = 0;
  11. PrefManager prefManager;
  12. ProgressLoader progressLoader;
  13. /*
  14. public packagingProductAdapter(List<packingProductPOJO> list_data, List<testPOJO> testPOJO, packingProduct context) {
  15. this.list_data = list_data;
  16. this.test_POJO = testPOJO;
  17. this.context = context;
  18. prefManager = new PrefManager(context);
  19. progressLoader = new ProgressLoader(context);
  20. } */
  21. public packagingProductAdapter(List<packingProductPOJO> list_data, List<testPOJO> test_POJO, testClass context) {
  22. this.list_data = list_data;
  23. this.test_POJO = test_POJO;
  24. this.context = context;
  25. prefManager = new PrefManager(context);
  26. progressLoader = new ProgressLoader(context);
  27. }
  28. @Override
  29. public packagingProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  30. View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.packing_product_cart_view_row,parent,false);
  31. return new packagingProductAdapter.ViewHolder(view);
  32. }
  33. @Override
  34. public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
  35. final packingProductPOJO listData = list_data.get(position);
  36. final testPOJO test_POJO1 = test_POJO.get(position);
  37. holder.setIsRecyclable(false);
  38. if (test_POJO.equals("null")){
  39. holder.linearLayout.setVisibility(View.GONE);
  40. }else
  41. {
  42. holder.linearLayout.setVisibility(View.VISIBLE);
  43. holder.heading.setText(test_POJO1.getContact_name());
  44. // holder.linearLayout.setBackgroundResource((R.drawable.rectangleborder));
  45. }
  46. holder.produt_quantity.setText("= " +listData.getQuantity() );
  47. if (listData.getProductPdf().equals("null")) {
  48. holder.product_name.setText(listData.getCat_name());
  49. }else {
  50. holder.product_name.setText("DOWNLOAD PDF");
  51. }
  52. product_id = listData.getProductid();
  53. category_id = listData.getImage();
  54. holder.purchaseQty.setText(listData.getUser_quantity());
  55. holder.pro_price.setText("Rs. " +listData.getPrice());
  56. final String finalPro_qty = "0";
  57. final String finalProduct_ID = product_id;
  58. holder.count_up.setOnClickListener(new View.OnClickListener() {
  59. @Override
  60. public void onClick(View view) {
  61. // ...
  62. }
  63. });
  64. holder.count_down.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View view) {
  67. // ...
  68. }
  69. });
  70. holder.purchaseQty.setOnClickListener(new View.OnClickListener() {
  71. @Override
  72. public void onClick(View v) {
  73. // ...
  74. }
  75. });
  76. }
  77. @Override
  78. public int getItemCount() {
  79. return list_data.size() + test_POJO.size();
  80. }
  81. public class ViewHolder extends RecyclerView.ViewHolder{
  82. private CardView cardView;
  83. private TextView purchaseQty, product_name, produt_quantity, heading, pro_price;
  84. private ImageView count_up, count_down;
  85. private LinearLayout topview, userQuantityLayout;
  86. private LinearLayout linearLayout;
  87. public ViewHolder(View itemView) {
  88. super(itemView);
  89. heading = itemView.findViewById(R.id.heading);
  90. purchaseQty= itemView.findViewById(R.id.txtItemQty);
  91. produt_quantity= itemView.findViewById(R.id.total_qty);
  92. product_name=itemView.findViewById(R.id.pro_name);
  93. purchaseQty.setText("0");
  94. count_up = itemView.findViewById(R.id.count_up);
  95. count_down = itemView.findViewById(R.id.count_down);
  96. pro_price = itemView.findViewById(R.id.pro_price);
  97. cardView = itemView.findViewById(R.id.packing_card_view);
  98. cardView.setVisibility(View.VISIBLE);
  99. topview = itemView.findViewById(R.id.topview);
  100. topview.setVisibility(View.GONE);
  101. linearLayout = itemView.findViewById(R.id.linearheading);
  102. }
  103. }
  104. }
  105. And this is Log error
  106. E/AndroidRuntime: FATAL EXCEPTION: main
  107. Process: com.cls.kraftpaper, PID: 24722
  108. java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
  109. at java.util.ArrayList.get(ArrayList.java:437)
  110. at com.kraftpaper.handler.activity.packaging.adapter.packagingProductAdapter.onBindViewHolder(packagingProductAdapter.java:71)
  111. at com.kraftpaper.handler.activity.packaging.adapter.packagingProductAdapter.onBindViewHolder(packagingProductAdapter.java:27)
  112. at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
  113. at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
  114. // ... (more lines of the error stack trace)
  115. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)

(Note: I've only translated the code and the error message as per your request. If you have any specific questions or need further assistance, feel free to ask.)

英文:

I want to use two arraylist in same adapter but I'm getting error. It is not setting the value and I'm getting INDEX OUT BOUND EXPECTION

This is my adapter code:

  1. public class packagingProductAdapter extends RecyclerView.Adapter&lt;packagingProductAdapter.ViewHolder&gt; {
  2. private List&lt;packingProductPOJO&gt; list_data;
  3. // private List&lt;packingProductPOJO&gt; testPOJO;
  4. private List&lt;testPOJO&gt; test_POJO;
  5. // private packingProduct context;
  6. private testClass context;
  7. String product_id, category_id, quantity = &quot;0&quot;, sizeValue = &quot;0&quot;;
  8. private static int _value = 0;
  9. PrefManager prefManager;
  10. ProgressLoader progressLoader;
  11. /*
  12. public packagingProductAdapter(List&lt;packingProductPOJO&gt; list_data, List&lt;testPOJO&gt; testPOJO, packingProduct context) {
  13. this.list_data = list_data;
  14. this.test_POJO = testPOJO;
  15. this.context = context;
  16. prefManager = new PrefManager(context);
  17. progressLoader = new ProgressLoader(context);
  18. } */
  19. public packagingProductAdapter(List&lt;packingProductPOJO&gt; list_data, List&lt;testPOJO&gt; test_POJO, testClass context) {
  20. this.list_data = list_data;
  21. this.test_POJO = test_POJO;
  22. this.context = context;
  23. prefManager = new PrefManager(context);
  24. progressLoader = new ProgressLoader(context);
  25. }
  26. @Override
  27. public packagingProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  28. View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.packing_product_cart_view_row,parent,false);
  29. return new packagingProductAdapter.ViewHolder(view);
  30. }
  31. @Override
  32. public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
  33. final packingProductPOJO listData = list_data.get(position);
  34. final testPOJO test_POJO1 = test_POJO.get(position);
  35. holder.setIsRecyclable(false);
  36. if (test_POJO.equals(&quot;null&quot;)){
  37. holder.linearLayout.setVisibility(View.GONE);
  38. }else
  39. {
  40. holder.linearLayout.setVisibility(View.VISIBLE);
  41. holder.heading.setText(test_POJO1.getContact_name());
  42. // holder.linearLayout.setBackgroundResource((R.drawable.rectangleborder));
  43. }
  44. holder.produt_quantity.setText(&quot;= &quot; +listData.getQuantity() );
  45. if (listData.getProductPdf().equals(&quot;null&quot;)) {
  46. holder.product_name.setText(listData.getCat_name());
  47. }else {
  48. holder.product_name.setText(&quot;DOWNLOAD PDF&quot;);
  49. }
  50. // holder.heading.setText(listData.getImage());
  51. product_id = listData.getProductid();
  52. category_id = listData.getImage();
  53. holder.purchaseQty.setText(listData.getUser_quantity());
  54. holder.pro_price.setText(&quot;Rs. &quot; +listData.getPrice());
  55. final String finalPro_qty = &quot;0&quot;;
  56. final String finalProduct_ID = product_id;
  57. holder.count_up.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. String removeProductId = listData.getId();
  61. String removeQuantity = listData.getUser_quantity();
  62. int quantity = 0;
  63. try{
  64. quantity = Integer.parseInt(removeQuantity) + 1;
  65. } catch(NumberFormatException ex){ // handle your exception
  66. ex.printStackTrace();
  67. }
  68. if (quantity &gt; Integer.parseInt(listData.getQuantity())){
  69. Toast.makeText(context, &quot;Insufficent Stock &quot;, Toast.LENGTH_LONG).show();
  70. }else {
  71. // context.add(&quot;pk@gmail.com&quot;, String.valueOf(quantity), finalProduct_ID, &quot;manual&quot;, category_id, holder.purchaseQty, position, &quot;0&quot;);
  72. }
  73. }
  74. });
  75. holder.count_down.setOnClickListener(new View.OnClickListener() {
  76. @Override
  77. public void onClick(View view) {
  78. String removeProductId = listData.getId();
  79. String removeQuantity = listData.getUser_quantity();
  80. final int quantity = Integer.valueOf(removeQuantity) - 1;
  81. // context.add(&quot;pk@gmail.com&quot;, String.valueOf(quantity), finalProduct_ID, &quot;manual&quot;, category_id, holder.purchaseQty, position, &quot;0&quot;);
  82. }
  83. });
  84. holder.purchaseQty.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. // get alert_dialog.xml view
  88. LayoutInflater li = LayoutInflater.from(context);
  89. View promptsView = li.inflate(R.layout.alert_dialog, null);
  90. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  91. context);
  92. // set alert_dialog.xml to alertdialog builder
  93. alertDialogBuilder.setView(promptsView);
  94. final EditText userSize = (EditText) promptsView.findViewById(R.id.etUserInput);
  95. final TextView enterQuantity = promptsView.findViewById(R.id.enterQuantity);
  96. // set dialog message
  97. alertDialogBuilder
  98. .setCancelable(false)
  99. .setPositiveButton(&quot;OK&quot;, new DialogInterface.OnClickListener() {
  100. public void onClick(DialogInterface dialog, int id) {
  101. // Toast.makeText(context, &quot;Entered: &quot;+userInput.getText().toString(), Toast.LENGTH_LONG).show();
  102. final String quantityValue = (userSize.getText().toString());
  103. if (TextUtils.isEmpty(quantityValue)) {
  104. userSize.setError(&quot;Quantity cannot be Empty&quot;);
  105. Toast.makeText(context, &quot;Quantity cannot be Empty&quot;, Toast.LENGTH_LONG).show();
  106. userSize.requestFocus();
  107. return;
  108. }
  109. final int quantity = Integer.valueOf(quantityValue);
  110. if (quantity &gt; Integer.parseInt(listData.getQuantity())){
  111. Toast.makeText(context, &quot;Insufficent Stock &quot;, Toast.LENGTH_LONG).show();
  112. }else {
  113. // context.add(&quot;pk@gmail.com&quot;, String.valueOf(quantity), finalProduct_ID, &quot;manual&quot;, category_id, holder.purchaseQty, position, sizeValue);
  114. }
  115. }
  116. })
  117. .setNegativeButton(&quot;Cancel&quot;,
  118. new DialogInterface.OnClickListener() {
  119. public void onClick(DialogInterface dialog, int id) {
  120. dialog.cancel();
  121. }
  122. });
  123. // create alert dialog
  124. AlertDialog alertDialog = alertDialogBuilder.create();
  125. // show it
  126. alertDialog.show();
  127. }
  128. });
  129. }
  130. @Override
  131. public int getItemCount() {
  132. return list_data.size() + test_POJO.size();
  133. }
  134. public class ViewHolder extends RecyclerView.ViewHolder{
  135. private CardView cardView;
  136. private TextView purchaseQty, product_name, produt_quantity, heading, pro_price;
  137. private ImageView count_up, count_down;
  138. private LinearLayout topview, userQuantityLayout;
  139. private LinearLayout linearLayout;
  140. public ViewHolder(View itemView) {
  141. super(itemView);
  142. heading = itemView.findViewById(R.id.heading);
  143. purchaseQty= itemView.findViewById(R.id.txtItemQty);
  144. produt_quantity= itemView.findViewById(R.id.total_qty);
  145. product_name=itemView.findViewById(R.id.pro_name);
  146. purchaseQty.setText(&quot;0&quot;);
  147. count_up = itemView.findViewById(R.id.count_up);
  148. count_down = itemView.findViewById(R.id.count_down);
  149. pro_price = itemView.findViewById(R.id.pro_price);
  150. cardView = itemView.findViewById(R.id.packing_card_view);
  151. cardView.setVisibility(View.VISIBLE);
  152. // purchaseQty.setBackground(context.getResources().getDrawable(R.drawable.rectangleborder));
  153. topview = itemView.findViewById(R.id.topview);
  154. topview.setVisibility(View.GONE);
  155. linearLayout = itemView.findViewById(R.id.linearheading);
  156. }
  157. }
  158. }

And this is Log error

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.cls.kraftpaper, PID: 24722
  3. java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
  4. at java.util.ArrayList.get(ArrayList.java:437)
  5. at com.kraftpaper.handler.activity.packaging.adapter.packagingProductAdapter.onBindViewHolder(packagingProductAdapter.java:71)
  6. at com.kraftpaper.handler.activity.packaging.adapter.packagingProductAdapter.onBindViewHolder(packagingProductAdapter.java:27)
  7. at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
  8. at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
  9. at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
  10. at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
  11. at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
  12. at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
  13. at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
  14. at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
  15. at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
  16. at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
  17. at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
  18. at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
  19. at android.view.View.measure(View.java:24845)
  20. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  21. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
  22. at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
  23. at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
  24. at android.view.View.measure(View.java:24845)
  25. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  26. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
  27. at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
  28. at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
  29. at android.view.View.measure(View.java:24845)
  30. at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1472)
  31. at android.widget.FrameLayout.onMeasure(FrameLayout.java:221)
  32. at android.widget.ScrollView.onMeasure(ScrollView.java:498)
  33. at android.view.View.measure(View.java:24845)
  34. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  35. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
  36. at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
  37. at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
  38. at android.view.View.measure(View.java:24845)
  39. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  40. at android.widget.FrameLayout.onMeasure(FrameLayout.java:221)
  41. at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
  42. at android.view.View.measure(View.java:24845)
  43. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  44. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
  45. at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
  46. at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
  47. at android.view.View.measure(View.java:24845)
  48. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  49. at android.widget.FrameLayout.onMeasure(FrameLayout.java:221)
  50. at android.view.View.measure(View.java:24845)
  51. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  52. at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
  53. at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
  54. at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
  55. at android.view.View.measure(View.java:24845)
  56. at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6908)
  57. at com.android.internal.policy.DecorView.measureChildWithMargins(DecorView.java:3029)
  58. at android.widget.FrameLayout.onMeasure(FrameLayout.java:221)
  59. at com.android.internal.policy.DecorView.onMeasure(DecorView.java:863)
  60. at android.view.View.measure(View.java:24845)
  61. at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3218)
  62. at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2004)
  63. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2296)
  64. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
  65. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7908)
  66. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1101)
  67. at android.view.Choreographer.doCallbacks(Choreographer.java:917)
  68. at android.view.Choreographer.doFrame(Choreographer.java:847)
  69. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1086)
  70. at android.os.Handler.handleCallback(Handler.java:883)
  71. at android.os.Handler.dispatchMessage(Handler.java:100)
  72. at android.os.Looper.loop(Looper.java:230)
  73. at android.app.ActivityThread.main(ActivityThread.java:7778)
  74. at java.lang.reflect.Method.invoke(Native Method)
  75. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
  76. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)

Here I have added the logcat. I'm getting the result is there.

答案1

得分: 1

因为这段代码而导致这个问题:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size() + test_POJO.size();
  4. }

你在返回两个列表的元素总和:
例如,假设你返回:return 3 + 3 // 总计 = 6

但是你还试图在以下位置访问各个列表:

  1. @Override
  2. public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
  3. final packingProductPOJO listData = list_data.get(position);
  4. final testPOJO test_POJO1 = test_POJO.get(position);

随着你滚动列表,位置会增加6次,这是两个列表getItemCount()的总和,这将引发java.lang.IndexOutOfBoundsException: Index: 3, Size: 3错误。

尝试只返回其中一个列表的大小:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size();
  4. }
英文:

That is because of this code:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size() + test_POJO.size();
  4. }

You are returning the sum of both lists:
For Eg let say you are returning: return 3 + 3 // total = 6

But also you are trying to access individual list in :

  1. @Override
  2. public void onBindViewHolder(final packagingProductAdapter.ViewHolder holder, final int position){
  3. final packingProductPOJO listData = list_data.get(position);
  4. final testPOJO test_POJO1 = test_POJO.get(position);

The position will increment as you scroll at list 6 times which is the total sum of the two lists getItemCount(), which will trow java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Try to return the size of only one of the lists:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size();
  4. }

答案2

得分: 1

你在getItemCount()方法中的代码如下:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size() + test_POJO.size();
  4. }

你返回了两个不同数组的大小。onBindViewHolder()方法有一个position变量。这个变量表示项目的位置,其范围是 0 到 getItemCount()

onBindViewHolder()方法中,你有以下代码:

  1. final packingProductPOJO listData = list_data.get(position);
  2. final testPOJO test_POJO1 = test_POJO.get(position);

你的getItemCount()方法返回了两个不同数组的大小,但你尝试访问相同的位置。这是异常的原因。如果数组大小相同,你可以使用以下代码:

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size();
  4. }

但如果不相同,你可以这样做:

  1. @Override
  2. public int getItemCount() {
  3. return Math.max(list_data.size(), test_POJO.size());
  4. }

onBindViewHolder()方法中:

  1. if (list_data.size() >= position) {
  2. list_data.get(0);
  3. } else {
  4. list_data.get(position);
  5. }
  6. if (test_POJO.size() >= position) {
  7. list_data.get(0);
  8. } else {
  9. test_POJO.get(position);
  10. }

但这不是一个好的方式。

英文:

Your code inside the getItemCount() method is

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size() + test_POJO.size();
  4. }

You returning 2 different array's size. onBindViewHolder() method has position variable. This variable gives item position and this position's range is 0..getItemCount().

In onBindViewHolder() method you have this code

  1. final packingProductPOJO listData = list_data.get(position);
  2. final testPOJO test_POJO1 = test_POJO.get(position);

Your getItemCount() method returning two different array's size but you trying access same position. This is Exception's cause. If the arrays size's are the same you can use this

  1. @Override
  2. public int getItemCount() {
  3. return list_data.size();
  4. }

But if not you can do this:

  1. @Override
  2. public int getItemCount() {
  3. return Math.max(list_data.size(), test_POJO.size());
  4. }

in onBindViewHolderMethod()

  1. if (list_data.size() &gt;= position) list_data.get(0);
  2. else list_data.get(position);
  3. if (test_POJO.size() &gt;= position) list_data.get(0);
  4. else test_POJO.get(possition);

But this is not the good way

答案3

得分: 0

尝试这样做。

  1. @Override
  2. public int getItemCount() {
  3. return min(list_data.size(), test_POJO.size());
  4. }

这将解决当前的问题。但你会发现RecyclerView中有一些数据丢失。

英文:

Try this.

  1. @Override
  2. public int getItemCount() {
  3. return min(list_data.size(), test_POJO.size());
  4. }

It will resolve current issue. But you'll find some data missing from RecyclerView.

答案4

得分: 0

如果这些列表包含相似的元素,您可以将它们合并,然后将它们作为适配器中的一个列表使用,这样做可以避免各种麻烦。

  1. List<PojoClass> combined = new ArrayList<>();
  2. combined.addAll(firstList);
  3. combined.addAll(secondList);
英文:

If the lists, contain similar elements, you could combine them and then use them as one list in the adapter, you will avoid all sorts of hassles if you do that.

  1. List&lt;PojoClass&gt; combined = new ArrayList&lt;&gt;();
  2. combined.addAll(firstList);
  3. combined.addAll(secondList);

huangapple
  • 本文由 发表于 2020年10月7日 12:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定