The CheckBox of ListView is selected/unselected, listviewcheckbox

Source: Internet
Author: User

The CheckBox of ListView is selected/unselected, listviewcheckbox

Define a HashMap list in the Adapter class to save whether each row is selected:

private static HashMap<Integer, Boolean> isSelected;

The HashMap list named isSelected is defined, and its getter and setter methods are defined as follows:

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected)   {          MyAdapter.isSelected = isSelected;      }        public static HashMap<Integer, Boolean> getIsSelected() {          return isSelected;      }

In the constructor of the Adapter class, set all the initial values of isSelected to false:

    public MyAdapter(Context context, List<String> list){        this.context = context;        this.list = list;                isSelected = new HashMap<Integer, Boolean>();        initData();    }        private void initData() {          for (int i = 0; i < list.size(); i++) {              getIsSelected().put(i, false);          }      }

In the getView method, define the CheckBox clicking method. When the CheckBox is selected or not, set the value of isSelected to true/false. At the same time, set whether the CheckBox is selected based on the value of isSelected, the corresponding icons are displayed:

        mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox);        mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if (isChecked){                    buttonView.setButtonDrawable(R.drawable.r02);                    isSelected.put(position, true);                }else{                    buttonView.setButtonDrawable(R.drawable.r01);                    isSelected.put(position, false);                }            }                    });        mCheckbox.setChecked(getIsSelected().get(position));        if (getIsSelected().get(position)){            mCheckbox.setButtonDrawable(R.drawable.r02);        }else{            mCheckbox.setButtonDrawable(R.drawable.r01);        }

In the main form file, When you click the CheckBox under the list, call the selectAll method to set all values of isSelected to true/false, and display the corresponding icon:

    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()){        case R.id.chkSelect:            selectAll(chkSelect.isChecked());            break;        case R.id.btnDelete:            break;        default:            break;        }            }
    private void selectAll(boolean x){        for (int i=0; i<mList.size(); i++){            mAdapter.getIsSelected().put(i, x);        }        if (chkSelect.isChecked()){            chkSelect.setButtonDrawable(R.drawable.r02);        }else{            chkSelect.setButtonDrawable(R.drawable.r01);        }        mAdapter.notifyDataSetChanged();    }

The complete file is as follows.

MainActivity. java:

Package com. hzhi. mylistview; import android. support. v7.app. actionBarActivity; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. checkBox; import android. widget. listView; public class MainActivity extends ActionBarActivity implements OnClickListener {private CheckBox chkSelect; private Button btnDelete; private List <String> mList; private MyAdapter mAdapter; private ListView mListview; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView (); initData (); mAdapter = new MyAdapter (MainActivity. this, mList); mListview. setAdapter (mAdapter);} private void initView () {chkSelect = (CheckBox) findViewById (R. id. chkSelect); btnDelete = (Button) findViewById (R. id. btnDelete); mListview = (ListView) findViewById (R. id. lstView); chkSelect. setOnClickListener (this); btnDelete. setOnClickListener (this); chkSelect. setButtonDrawable (R. drawable. r01);} private void initData () {mList = new ArrayList <String> (); for (int I = 0; I <10; I ++) {mList. add ("th" + String. valueOf (I) + "data") ;}} private void selectAll (boolean x) {for (int I = 0; I <mList. size (); I ++) {mAdapter. getIsSelected (). put (I, x);} if (chkSelect. isChecked () {chkSelect. setButtonDrawable (R. drawable. r02);} else {chkSelect. setButtonDrawable (R. drawable. r01);} mAdapter. notifyDataSetChanged () ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. chkSelect: selectAll (chkSelect. isChecked (); break; case R. id. btnDelete: break; default: break ;}}}

MyAdapter. java:

package com.hzhi.mylistview;import java.util.HashMap;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MyAdapter extends BaseAdapter{        List<String> list;    Context context;    private static HashMap<Integer, Boolean> isSelected;        public MyAdapter(Context context, List<String> list){        this.context = context;        this.list = list;                isSelected = new HashMap<Integer, Boolean>();        initData();    }        private void initData() {          for (int i = 0; i < list.size(); i++) {              getIsSelected().put(i, false);          }      }    @Override    public int getCount() {        // TODO Auto-generated method stub        return list.size();    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return list.get(position);    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public View getView(final int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        CheckBox mCheckbox;        TextView mText;                if(convertView == null){            convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);        }        mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox);        mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if (isChecked){                    buttonView.setButtonDrawable(R.drawable.r02);                    isSelected.put(position, true);                }else{                    buttonView.setButtonDrawable(R.drawable.r01);                    isSelected.put(position, false);                }            }                    });        mCheckbox.setChecked(getIsSelected().get(position));        if (getIsSelected().get(position)){            mCheckbox.setButtonDrawable(R.drawable.r02);        }else{            mCheckbox.setButtonDrawable(R.drawable.r01);        }                mText = (TextView)convertView.findViewById(R.id.txtText);        mText.setText(list.get(position).toString());                return convertView;    }        public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {          MyAdapter.isSelected = isSelected;      }        public static HashMap<Integer, Boolean> getIsSelected() {          return isSelected;      }}

The running effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.