[Android] listview-perfect combination with checkbox

Source: Internet
Author: User

The setting menu of the Android system is composed of a large listview, many of which contain checkbox, such as wireless network and flight mode.

If you want to make a similar application, you need to use the listview adapter.

In addition, we need to use hashmap ~

The function implemented in this article is to separate the checkbox and listview In the subitem of the listview, that is, click the subitem of the listview to view the content of the subitem of the listview, and click the checkbox in the subitem to select or not the checkbox, the listview listener we are doing here is system. the position of the clicked listview.

Initialize data

data = new ArrayList<HashMap>();for (int i = 0; i < 20; i++) {map = new HashMap();map.put("title", "title-->" + i);map.put("content", "content--" + i);data.add(map);}

Set listview

Adapter = new myadapter (checklist. this, data); mlistview. setadapter (adapter); // Add a click event for the subitem of listview. position is the original arg2mlistview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {system. Out. println ("the row number you clicked is:" + position );}});

Define the adapter we need

int count = scroll_num;Context mContext;ArrayList<HashMap> mData;LayoutInflater mInflater;public MyAdapter(Context context, ArrayList<HashMap> data) {this.mContext = context;this.mData = data;mInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);if (count > mData.size()) {count = mData.size();}isSelected = new HashMap<Integer, Boolean>();for (int i = 0; i < data.size(); i++) {isSelected.put(i, false);}}@Overridepublic int getCount() {return mData.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {if (convertView == null) {convertView = mInflater.inflate(R.layout.item_listview, null);holder = new ViewHolder();holder.content = (TextView) convertView.findViewById(R.id.item_listview_content);holder.checkBox = (CheckBox) convertView.findViewById(R.id.item_listview_checkbox);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.content.setText(data.get(position).get("content").toString());holder.checkBox.setChecked(isSelected.get(position));holder.checkBox.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (isSelected.get(position)) {isSelected.put(position, false);} else {isSelected.put(position, true);}notifyDataSetChanged();}});return convertView;}

Finally, define an XML adapter.

Engineering Resources: http://download.csdn.net/detail/etzmico/3780822

Related Article

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.