Get Selected Item Using Checkbox in Listview

前端 未结 9 720
醉酒成梦
醉酒成梦 2020-11-22 09:36

I am creating an Android application where I have a ListView that displays all of the applications that were installed in my mobile phone.

My ListView is customized,

9条回答
  •  盖世英雄少女心
    2020-11-22 09:48

    "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.

           
      
    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);
          }
        }
      }
      

提交回复
热议问题