Get Selected Item Using Checkbox in Listview

前端 未结 9 707
醉酒成梦
醉酒成梦 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.

           <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);
          }
        }
      }
      
    0 讨论(0)
  • 2020-11-22 09:49

    Full reference present at : listview with checkbox android studio Pass selected items to next activity

    Main source code is as below.

    Create a model class first

    public class Model {
    
        private boolean isSelected;
        private String animal;
    
        public String getAnimal() {
            return animal;
        }
    
        public void setAnimal(String animal) {
            this.animal = animal;
        }
    
        public boolean getSelected() {
            return isSelected;
        }
    
        public void setSelected(boolean selected) {
            isSelected = selected;
        }
    }
    

    Then in adapter class, setTags to checkbox. Use those tags in onclicklistener of checkbox.

    public class CustomAdapter  extends BaseAdapter {
    
        private Context context;
        public static ArrayList<Model> modelArrayList;
    
    
        public CustomAdapter(Context context, ArrayList<Model> modelArrayList) {
    
            this.context = context;
            this.modelArrayList = modelArrayList;
    
        }
    
        @Override
        public int getViewTypeCount() {
            return getCount();
        }
        @Override
        public int getItemViewType(int position) {
    
            return position;
        }
    
        @Override
        public int getCount() {
            return modelArrayList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return modelArrayList.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
    
            if (convertView == null) {
                holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.lv_item, null, true);
    
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb);
                holder.tvAnimal = (TextView) convertView.findViewById(R.id.animal);
    
                convertView.setTag(holder);
            }else {
                // the getTag returns the viewHolder object set as a tag to the view
                holder = (ViewHolder)convertView.getTag();
            }
    
    
            holder.checkBox.setText("Checkbox "+position);
            holder.tvAnimal.setText(modelArrayList.get(position).getAnimal());
    
            holder.checkBox.setChecked(modelArrayList.get(position).getSelected());
    
            holder.checkBox.setTag(R.integer.btnplusview, convertView);
            holder.checkBox.setTag( position);
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview);
                    TextView tv = (TextView) tempview.findViewById(R.id.animal); 
                    Integer pos = (Integer)  holder.checkBox.getTag();
                    Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show();
    
                    if(modelArrayList.get(pos).getSelected()){
                        modelArrayList.get(pos).setSelected(false);
                    }else {
                        modelArrayList.get(pos).setSelected(true);
                    }
    
                }
            });
    
            return convertView;
        }
    
        private class ViewHolder {
    
            protected CheckBox checkBox;
            private TextView tvAnimal;
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:51

    make the checkbox non-focusable, and on list-item click do this, here codevalue is the position.

        Arraylist<Integer> selectedschools=new Arraylist<Integer>();
    
        lvPickSchool.setOnItemClickListener(new AdapterView.OnItemClickListener() 
      {
    
       @Override
            public void onItemClick(AdapterView<?> parent, View view, int codevalue, long id)
       {
                          CheckBox cb = (CheckBox) view.findViewById(R.id.cbVisitingStatus);
    
                cb.setChecked(!cb.isChecked());
                if(cb.isChecked())
                {
    
                    if(!selectedschool.contains(codevaule))
                    {
                        selectedschool.add(codevaule);
                    }
                }
                else
                {
                    if(selectedschool.contains(codevaule))
                    {
                        selectedschool.remove(codevaule);
                    }
                }
            }
        });
    
    0 讨论(0)
  • 2020-11-22 09:52

    You have to add an OnItemClickListener to the listview to determine which item was clicked, then find the checkbox.

    mListView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox_id);
        }
    });
    
    0 讨论(0)
  • 2020-11-22 09:53

    It's a simplifications but very easy... You need to add the the focusable flag to the checkbox, as written before. You need also to add the clickable flag, as shown here:

    android:focusable="false"
    android:clickable="false"
    

    Than you control the checkbox state from within the ListView (ListFragment in my case) onListItemClick event.

    This the sample onListItemClick method:

    public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //Get related checkbox and change flag status..
    CheckBox cb = (CheckBox)v.findViewById(R.id.rowDone);
    cb.setChecked(!cb.isChecked());
    Toast.makeText(getActivity(), "Click item", Toast.LENGTH_SHORT).show();
    }
    
    0 讨论(0)
  • 2020-11-22 09:53

    [Custom ListView with CheckBox]

    If customlayout use checkbox, you must set checkbox focusable = false

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    
      <TextView android:id="@+id/rowTextView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp" >
      </TextView>
    
      <CheckBox android:id="@+id/CheckBox01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="10dp"
        android:layout_alignParentRight="true" 
        android:layout_marginRight="6sp"
        android:focusable="false">           // <---important
      </CheckBox>
    
    </RelativeLayout>
    

    Readmore : A ListView with Checkboxes (Without Using ListActivity)

    0 讨论(0)
提交回复
热议问题