[Android Basics] The status of the CheckBox check box is changed when SimpleAdapter is rewritten. androidcheckbox
When using a custom ListView, you often need to rewrite the Adapter based on SimpleAdapter to implement button response based on the custom Adapter in the previous article, and implement the response CheckBox today.
Like in the previous article, we need to add a MainActivity variable to the overwritable MySimpleAdapter to call the data method in the MainActivity change List <Map <String, Object> so that the Refresh can be achieved.
The purpose of ListView. The Code is as follows:
This is how MainActivity calls the List <Map <String, Object> variable in the MainActivity to change the data and refresh the ListView, the input parameter is the serial number of the Item in ListView and the Boolean value changed (because only the CheckBox is modified ):
public void setChecked(int position,boolean m){ Map<String,Object> map = today_plan_listItems.get(position); map.put("check", m); listItems.set(position, map); listView.invalidate(); }
ListItems is a List <Map <String, Object> variable,
ListView is a ListView variable.
The following is the full code of the MySimpleAdapter class:
Package com. plan; import java. util. list; import java. util. map; import android. content. context; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. toast; import android. widget. compoundButton. onCheckedChangeListener; impo Rt android. widget. SimpleAdapter; import com. example. plan. R; public class MySimpleAdapter extends SimpleAdapter {MainActivity context; public MySimpleAdapter (Context context, List <? Extends Map <String,?> Data, int resource, String [] from, int [] to) {super (context, data, resource, from, to); this. context = (MainActivity) context; // TODO Auto-generated constructor stub} public View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub View v = super. getView (position, convertView, parent); Button btn = (Button) v. findViewById (R. id. button1); final CheckBox che Ck = (CheckBox) v. findViewById (R. id. checkBox_complish); final int p = position; Log. d ("Position", Integer. toString (position); // check the response of the check box. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub String s = (p + ":" + check. isChecked (); Toast. makeText (context, s, 2000 ). show (); if (check. isChecked () {context. setChecked (p, true);} else if (! Check. isChecked () {context. setChecked (p, false) ;}}); btn. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub // Toast. makeText (context, "delete" + Integer. toString (p), 2000 ). show (); context. deleteItemsTodayPlanList (p) ;}}); return v ;}}
The check response of the check box listens to its OnClickListner event. The original status is false, and the result is true after you click it.
Note: It is best not to listen to the OnCheckedChangeListener event here, because once this listener is used, the status changes every time the ListView is refreshed, which can be very troublesome to handle.