[android] Get Selected Item Using Checkbox in Listview

"The use of the checkbox is to determine what Item in the Listview that I selected"

  1. Just add the tag to checkbox using setTag() method in the Adapter class. and other side using getTag() method.

          @Override
       public void onBindViewHolder(MyViewHolder holder, int position) {
    
    ServiceHelper helper=userServices.get(position);
    holder.tvServiceName.setText(helper.getServiceName());
    
    if(!helper.isServiceStatus()){
    
        holder.btnAdd.setVisibility(View.VISIBLE);
        holder.btnAdd.setTag(helper.getServiceName());
        holder.checkBoxServiceStatus.setVisibility(View.INVISIBLE);
    
    }else{
    
        holder.checkBoxServiceStatus.setVisibility(View.VISIBLE);
           //This Line
        holder.checkBoxServiceStatus.setTag(helper.getServiceName());
        holder.btnAdd.setVisibility(View.INVISIBLE);
    
       }
    
    }
    
  2. In xml code of the checkbox just put the "android:onClick="your method""attribute.

         <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="checkboxClicked"  
                android:id="@+id/checkBox_Service_row"
                android:layout_marginRight="5dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />
    
  3. In your class Implement that method "your method".

         protected void checkboxClicked(View view)
          {
    
            CheckBox checkBox=(CheckBox) view;
              String tagName="";
             if(checkBox.isChecked()){
               tagName=checkBox.getTag().toString();
               deleteServices.add(tagName);
               checkboxArrayList.add(checkBox);
            }else {
                   checkboxArrayList.remove(checkBox);
                   tagName=checkBox.getTag().toString();
                        if(deleteServices.size()>0&&deleteServices.contains(tagName)){
     deleteServices.remove(tagName);
        }
      }
    }