Ideas
1 Trigger Event Setonitemclicklistener change checkbox
2 Change the checkbox status in the adapter's GetView () to record the selected checkbox position
3 Adapter provides method change checkbox
4 Start GetView with notifydatasetchanged ()
Implementation code
//Adapter Public class myadapter extends baseadapter { //Fill list of data Privatearraylist<map<string, object>> list;//To control the check status of the checkbox PrivateHashmap<integer,boolean> isSelected;//Context PrivateContext context;//To import layouts PrivateLayoutinflater Inflater =NULL;//Builder Public Myadapter(arraylist<map<string, object>> list, context context) { This. Context = Context; This. list = list; Inflater = Layoutinflater.from (context); isSelected =NewHashmap<integer, boolean> ();//Initialize dataInitdate (); }//Initialize the isselected data Private void initdate(){ for(intI=0; I<list.size (); i++) {getisselected (). put (I,false); } }@Override Public int GetCount() {returnList.size (); }@Override PublicObjectGetItem(intPosition) {returnList.get (position); }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {//import layouts and assign values to ConvertviewConvertview = Inflater.inflate (r.layout.item_exchange_self,NULL); TextView tv_name = (TextView) Convertview.findviewbyid (r.id.exchange_self_name); TextView tv_address = (TextView) Convertview.findviewbyid (r.id.exchange_self_address); TextView tv_date = (TextView) Convertview.findviewbyid (r.id.exchange_self_date); CheckBox checkbox = (checkbox) Convertview.findviewbyid (r.id.select_address);//Set the display of TextView in listTv_name.settext (List.get (position). Get ("Name"). ToString ()); Tv_address.settext (List.get (position). Get ("Address"). ToString ()); Tv_date.settext (List.get (position). Get ("Workdates"). ToString ());//Set checkbox selection according to isselectedCheckbox.setchecked (getisselected (). get (position));returnConvertview; } PublicHashmap<integer,boolean>getisselected() {returnisSelected; } Public void setisselected(intPostiton) {initdate (); This. Isselected.put (Postiton,true); Notifydatasetchanged (); } }
//listview ListView Exchangecenters = (ListView) Findviewbyid (r.id.exchange_centers); final Myadapter myadapter = new myadapter (GetData (), this ); Exchangecenters.setadapter (Myadapter); Exchangecenters.setonitemclicklistener (new Adapterview.onitemclicklistener () { @Override public void onitemclick (adapterview<?> parent, view view, int position, Long ID) {myadapter.setisselected (position); Exchangecenter = GetData (). get (position); } });
Considerations for Layout
1.CheckBox own Click to close, add property android:clickable= "false"
2.item Headquarters Bureau needs to add android:descendantfocusability= "blocksdescendants" to ensure that the ListView click event is OK
3.ListView Property plus android:layout_weight= "1" ensures that the button below is not covered
Android ListView Radio CheckBox