Android adds a checkbox in the ListView to implement a single-select multi-select operation problem

Source: Internet
Author: User

Android changes different backgrounds depending on the state of the view
Http://www.eoeandroid.com/thread-198029-1-1.html

Android mimics the menu effect launched by Friends Web "improved version"
Http://www.eoeandroid.com/thread-198019-1-1.html

Let server IIS support the Setup method for the. apk file Download
Http://www.eoeandroid.com/thread-198033-1-1.html

In some Android development requirements, it is sometimes necessary to add a checkbox in the LISTVEIW to implement the single-selection, multi-select operation. Seemingly just changing the checkbox is as simple as it is, but in real development it's not so handy. Especially when the ListView is more (for example, the screen can only display a maximum of 10 item, but there are a total of 12 item, that is, the ListView item number is larger than the screen can display the item number) when sliding the screen, because the adapter GetView () The item removed from the screen will be reused, causing the checkbox selection to be unhealthy. Oneself in development encountered such a problem is very distressed, looked up the information, found that there is little on the internet for this kind of bulk operation does not have a complete example. Search a lot of posts to perfect the implementation of this common operation. So here to put this demo, for your reference, I hope to be helpful to everyone.
Layout of the main interface main.xml that's not much to say.

<?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" Androidoid: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= "213dip" android:text= "Cancel selected" android:gravity= "center"/> </RelativeLayout> </linearlayout >
The layout of each item in the ListView, Listviewitem.xml: It is important to note that since the Click event Priority of the checkbox is higher than the ListView, the android:focusable= "false" attribute is added. Causes the checkbox to start without getting the focus. In addition here is the click of the ListView item to control the status change of the checkbox, that is, to have the item receive the Clik event, so you need to add Android:focusableintouchmode= "false" this property.
<?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;}

For the ListView custom adapter, this class is the inner class for the mainactivity.java of the Activity class

public static class Myadapter extends Baseadapter {public static hashmap<integer, boolean> isSelected; private Con Text context = NULL; Private Layoutinflater inflater = null; Private list

Finally, the most important thing is the handling of some incident response 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

I chose the G2, G3, G11 three items, now the screen swipe to the bottom, to see the state saved very well, TextView show that 3 items are selected. Select all, reverse select, deselect all function Normal, multi-select operation Perfect Solution!

Android adds a checkbox in the ListView to implement a single-select multi-select operation problem

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.