It perfectly solves the problem of batch operations by adding checkbox to listview by Android.

Source: Internet
Author: User

From: http://www.eoeandroid.com/forum.php? MoD = viewthread & tid = 152037.

In some Android development requirements, you sometimes need to add checkbox to listveiw to implement Single-choice and multiple-choice operations. On the surface, it seems as simple as changing the checkbox. However, in actual development, implementation is not so handy. Especially when a large number of listview slides (for example, the screen can only display up to 10 items, but there are a total of 12 items, that is, the number of items in the listview is greater than the number of items that can be displayed on the screen, because getview () in the adapter repeatedly uses items on the removed screen, the checkbox selection status may be abnormal. I encountered such a problem during development. I checked the relevant information and found that there was very few examples of such batch operations on the Internet. I searched many posts to achieve this common operation perfectly. So here we will post this demo for your reference, hoping to help you.

The layout of the main interface is main. xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> <linearlayout Android: orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <textview Android: Id = "@ + ID/TV" Android: layout_width = "fill_parent" Android: layout_height = "50dip" Android: textcolor = "# fcfcfc" Android: textsize = "11pt" Android: gravity = "center_vertical" Android: layout_marginleft = "10dip"/> <listview Android: Id = "@ + ID/LV" Android: layout_width = "fill_parent" Android: layout_height = "381dip" Android: cachecolorhint = "#00000000"> </listview> </linearlayout> <relativelayout Android: layout_width = "fill_parent" Android: layout_height = "53dip" Android: orientation = "horizontal"> <button Android: Id = "@ + ID/selectall" Android: layout_width = "80dip" Android: layout_height = "50dip" Android: layout_marginleft = "20dip" Android: text = "select all" Android: gravity = "center"/> <button Android: Id = "@ + ID/inverseselect" Android: layout_width = "80dip" Android: layout_height = "50dip" Android: layout_marginleft = "118dip" Android: text = "" Android: gravity = "center"/> <button Android: id = "@ + ID/cancel" Android: layout_width = "80dip" Android: layout_height = "50dip" Android: layout_marginleft = "paidip" Android: text = "cancel selected" Android: gravity = "center"/> </relativelayout> <linearlayout Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <textview Android: layout_width = "fill_parent" Android: layout_height = "100dip" Android: text = "Original: simtice QQ: 512375320" Android: layout_marginleft = "10dip"/> </linearlayout>

The layout of each item in listview, listviewitem. xml:

Note that because the checkbox click event has a higher priority than the listview, you need to add the Android: focusable = "false" attribute so that the checkbox does not get the focus at the beginning.

In addition, click the item in listview to control the status change of the checkbox, that is, to allow the item to receive clik events. Therefore, you need to add the Android: focusableintouchmode = "false" attribute.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="55dip"    android:orientation="horizontal"    android:layout_marginTop="20dip"    >         <TextView             android:id="@+id/item_tv"            android:layout_width="267dip"        android:layout_height="40dip"            android:textSize="10pt"            android:gravity="center_vertical"            android:layout_marginLeft="10dip"            />    <CheckBox         android:id="@+id/item_cb"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:focusable="false"            android:focusableInTouchMode="false"            android:clickable="false"          android:layout_toRightOf="@id/item_tv"           android:layout_alignParentTop="true"        android:layout_marginRight="5dip"               /></RelativeLayout >

Viewholder class

package simtice.test.listview.viewholder;import android.widget.CheckBox;import android.widget.TextView;public class ViewHolder {        public TextView tv = null;        public CheckBox cb = null;}

Is the listview custom adapter, which is the internal class of mainactivity. java.

Public static class myadapter extends baseadapter {public static hashmap <integer, Boolean> isselected; private context = NULL; private layoutinflater Inflater = NULL; private list 

Finally, the most important thing is the processing of some event responses in mainactivity. java.

Public class mainactivity extends activity {textview TV = NULL; listview Lv = NULL; button btn_selectall = NULL; button btn_inverseselect = NULL; button btn_calcel = NULL; string name [] = {"g1", "G2", "G3", "G4", "G5", "G6", "G7", "G8 ", "G9", "G10", "G11", "G12", "G13", "G14"}; arraylist <string> liststr = NULL; private list 

Okay. Let's see the running result.

I have selected three items: G2, G3, and G11. Now the screen slides to the bottom and the status is saved very well. textview displays three items selected. The select all, select inverse, and cancel selected functions are normal. The multi-choice operation is perfect!

Listviewforcheckbox.rar(65.9
KB, downloads: 246)

Upload

Click the file name to download the attachment.
Download points: ECOIN-1 RMB

With the help of a few friends, I feel that although the function has been implemented, the code is still very problematic in terms of design and writing. Thanks to the Code modified by my friend on the 38 th floor, I think the code is very beautiful and I made a slight modification, which is more perfect. Thank you for your advice.
Listviewcheckbox1.rar(93.43
KB, downloads: 502)

In addition: http://blog.sina.com.cn/s/blog_79014b2301017ejd.html

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.