vendredi 14 juin 2019

Android checkbox checked at random position in Listview

I know it is common problem with Listview and Checkbox, I am try from 2 days but it is not solving, so I raised question here.

I had list of items in Arraylist, like 15(supposed) items. Inside listview there is a checkbox, by default 11(supposed) checkbox is comes checked by API (JSON Response) But I have to show only 5 items checked. I tried alot but not find any solution.

I had create a dummy list that contain 11 items that comes from API and save 5 items from original arraylist. Here is code to match items in listview and save this list in Shared Preference.

Adapter class inside getViwe() function

 List<SaveOrderId> getIds = prefManager.getNewOrders(context);

 for (int i = 0; i < getIds.size(); i++) {

  String idget = getIds.get(i).getOrderId();// it get ids from save list

  for (int j = 0; j < newOrders.size(); j++) {

      String bot = newOrders.get(j).getId(); // id from orginal list

      if (bot.equalsIgnoreCase(idget)) {

          showErrorLog(TAG, String.format("Bot id is %s and Saved Id is %s", bot, idget));

          myViewHolder.add_payment.setChecked(true);

          final int sdk = android.os.Build.VERSION.SDK_INT;
          if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
              myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
          } else {
              myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_green));
          }
          myViewHolder.txt_check_status.setText("ADDED");

      } else {
          getPerspective().showErrorLog(TAG, "Not Equal");

          myViewHolder.add_payment.setChecked(false);
          final int sdk = android.os.Build.VERSION.SDK_INT;
          if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
              myViewHolder.txt_layout_checked.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
          } else {
              myViewHolder.txt_layout_checked.setBackground(ContextCompat.getDrawable(context, R.drawable.shape_rect_rounder_corner3_bg_dark_gray));
          }
          myViewHolder.txt_check_status.setText("ADD FOR PAYMENT");
      }
  }
}

Problem is that, all 11 items showing checked. Please help.




Aucun commentaire:

Enregistrer un commentaire