I want to implement recycle view with checkbox when I checked one checkbox it selecting two checkbox at a time. I have tried some option as well but no luck help me to resolve the problem. CheckBox is not working properly when I tried to select any item from the list Its selecting the two item from the list and when I un check the checkBox both are unchecked.
public class RecyclerViewAdapterDc extends RecyclerView.Adapter<RecyclerViewAdapterDc.ViewHolderDc> {
Context context;
// public ArrayList<String> checkBoxArrayList = new ArrayList<>();
List<DCModelClass> dcModelClassList = new ArrayList<>();
List<AllProductsOfBag> allProductsOfBagsList = new ArrayList<>();
List<AllProductsofBagDcDeliver> allProductsofBagDcDeliverList = new ArrayList<>();
public List<AllProductsofBagDcDeliver> checkedallProductsofBagDcDeliverList = new ArrayList<>();
GlobalData globalData = new GlobalData();
// private final boolean mCheckStates[];
private ArrayList<Integer> selectCheck = new ArrayList<>();
public RecyclerViewAdapterDc(Context context, List<DCModelClass> dcModelClassList,
List<AllProductsofBagDcDeliver> allProductsofBagDcDeliverList) {
// List<AllProductsOfBag> allProductsOfBagsList
this.context = context;
this.dcModelClassList = dcModelClassList;
// this.allProductsOfBagsList = allProductsOfBagsList;
this.allProductsofBagDcDeliverList = allProductsofBagDcDeliverList;
// mCheckStates = new boolean[allProductsofBagDcDeliverList.size()];
}
@NonNull
@NotNull
@Override
public ViewHolderDc onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.custom_layout_dc_home, parent, false);
ViewHolderDc viewHolderDc = new ViewHolderDc(view);
return viewHolderDc;
}
@Override
public void onBindViewHolder(@NonNull @NotNull ViewHolderDc holder, int position) {
// holder.bindProduct(allProductsofBagDcDeliverList.get(position));
// if (dcModelClassList.size() > 0) {
// holder.bagNumber.setText(String.valueOf(dcModelClassList.get(position).getBaG_ID_NUMBER()));
//today
holder.waybill.setText(allProductsofBagDcDeliverList.get(position).getProducT_WAYBILL_NUMBER());
holder.ConsingeeName.setText(allProductsofBagDcDeliverList.get(position).getConsigneE_NAME());
holder.orderID.setText(allProductsofBagDcDeliverList.get(position).getReferencE_NO());
//todayend
// holder.shortAddress.setText(allProductsofBagDcDeliverList.get(position).get());
//today
if (allProductsofBagDcDeliverList.get(position).getProducT_PAYMENT_TYPE().contains("COD")) {
holder.ppOrCod.setText(String.valueOf(allProductsofBagDcDeliverList.get(position).getProducT_VALUE_AMOUNT()));
} else {
holder.ppOrCod.setText(allProductsofBagDcDeliverList.get(position).getProducT_PAYMENT_TYPE());
}
holder.shortAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openPopUpDialog(position);
}
private void openPopUpDialog(int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(" Full Address of Client")
.setMessage(allProductsofBagDcDeliverList.get(position).getAddress())
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create();
builder.show();
}
});
//todayend
holder.setItemSelectedListner(new IsSelectedClickListener() {
@Override
public void onIsSelectedClick(View view, int position) {
CheckBox chk = (CheckBox) view;
if (chk.isChecked()){
checkedallProductsofBagDcDeliverList.add(allProductsofBagDcDeliverList.get(position));
}else if(!chk.isChecked()){
checkedallProductsofBagDcDeliverList.remove(allProductsofBagDcDeliverList.get(position));
}
}
});
// holder.checkBox.setTag(position);
//// holder.chkSelect.setTag(position);
//// holder.checkBox.setChecked(mCheckStates.get(position, false));
//// holder.checkBox.setOnCheckedChangeListener(this);
//
// holder.checkBox.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// if (holder.checkBox.isChecked() == true) {
// checkBoxArrayList.add(allProductsofBagDcDeliverList.get(position).getProducT_WAYBILL_NUMBER());
// globalData.setWaybill(checkBoxArrayList);
// holder.constraintLayout.setBackgroundColor(Color.parseColor("#66bb6a"));
//
//
//
// //Log.d("GlobalData", globalData.getWaybill().toString());
// } else {
// // holder.assign.setVisibility(View.VISIBLE);
// checkBoxArrayList.remove(allProductsofBagDcDeliverList.get(position).getProducT_WAYBILL_NUMBER());
//// globalData.setWaybill(checkBoxArrayList);
// holder.constraintLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
//
// }
// }
// });
}
// }
@Override
public int getItemCount() {
return allProductsofBagDcDeliverList.size();
}
public void filterList(List<AllProductsofBagDcDeliver> filteredList) {
allProductsofBagDcDeliverList = filteredList;
notifyDataSetChanged();
}
public class ViewHolderDc extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView waybill, ConsingeeName, orderID, shortAddress, ppOrCod, bagNumber;
ImageButton clientPhoneButton;
ConstraintLayout constraintLayout;
CheckBox checkBox;
Button assign;
IsSelectedClickListener isSelectedClickListener;
public ViewHolderDc(@NonNull @NotNull View itemView) {
super(itemView);
// bagNumber = itemView.findViewById(R.id.idBagNumberDc);
waybill = itemView.findViewById(R.id.idWaybill);
ConsingeeName = itemView.findViewById(R.id.idName);
orderID = itemView.findViewById(R.id.idOrderid);
shortAddress = itemView.findViewById(R.id.idShortAddress);
ppOrCod = itemView.findViewById(R.id.idPaymentType);
// clientPhoneButton = itemView.findViewById(R.id.idImageButtonCall);
constraintLayout = itemView.findViewById(R.id.idConstrainLayoutCustomLayoutDc);
checkBox = itemView.findViewById(R.id.idCheckBoxDc);
checkBox.setOnClickListener(this);
}
public void setItemSelectedListner(IsSelectedClickListener ic){
this.isSelectedClickListener = ic;
}
@Override
public void onClick(View v) {
this.isSelectedClickListener.onIsSelectedClick(v,getLayoutPosition());
}
}
public interface IsSelectedClickListener {
void onIsSelectedClick(View view,int position);
}
}