I used to have a headache for this problem for a long time. And then went to work on other things. Have not been to tidy up, today very difficult to idle down. Just come up with this listview + checkbox problem
Interface:
Listview_cell:
The interface is simple, a full-screen Listview,cell is simple, a textview a checkbox
Activity
Package Com.example.testlistviewandcheckbox;import Java.util.arraylist;import Java.util.list;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.view.Menu;import Android.view.menuitem;import Android.widget.adapter;import Android.widget.listview;public class MainActivity Extends Activity {private Context context;private ListView mylistview;private list<string> list;private myadapter adapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview ();} private void Initview () {context = This;mylistview = (ListView) Findviewbyid (r.id.lv_test); list = new arraylist<string > (); for (int. i=0;i<20;i++) {List.add ("This is a title of:" +i);} adapter = new Myadapter (context, list); Mylistview.setadapter (adapter);}}
Adapter
Package Com.example.testlistviewandcheckbox;import Java.util.hashmap;import Java.util.list;import Android.content.context;import Android.view.view;import Android.view.viewgroup;import Android.widget.BaseAdapter; Import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.textview;public class MyAdapter Extends Baseadapter {private Context context;private list<string> list;private Hashmap<integer, boolean> Tempmap;public myadapter (Context context,list<string> List) {this.context = Context;this.list = List;tempMap = new Hashmap<integer,boolean> (); for (int i=0;i<list.size (); i++) {//Initialize data, just enter the words to set each to Falsetempmap.put (I, False );//If you do not initialize will be reported null pointer, after all, you do not have anything to take Caesar} @Overridepublic int GetCount () {return list==null?0:list.size ();} @Overridepublic String getItem (int position) {return list.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic View GEtview (final int position, View Convertview, ViewGroup parent) {if (convertview==null) {viewholder = new Viewholder (); conv Ertview = view.inflate (context, R.layout.listview_cell, null); Viewholder.tv_title = (TextView) Convertview.findviewbyid (r.id.tv_title); viewholder.cb_test = (CheckBox) Convertview.findviewbyid (R.id.cb_test); Convertview.settag (Viewholder);} else {Viewholder = (Viewholder) Convertview.gettag ();} ViewHolder.tv_title.setText (GetItem (position));//Add a check listener to the checkbox to save the status of the checkbox in the current position into a hashmap viewholder.cb_ Test.setoncheckedchangelistener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {tempmap.put (position, isChecked);//Store The state of the current position}});// Take our status value from the HashMap and assign it to the checkboxviewHolder.cb_test.setChecked (Tempmap.get (position)) corresponding to the ListView position; return Convertview;} Private Viewholder Viewholder;public class Viewholder{public TextView tv_title;public CheckBox cb_test;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android ListView + checkbox list swipe up and down causes check box status loss resolution