The setting menu of the Android system is composed of a large listview, many of which contain checkbox, such as wireless network and flight mode.
If you want to make a similar application, you need to use the listview adapter.
In addition, we need to use hashmap ~
The function implemented in this article is to separate the checkbox and listview In the subitem of the listview, that is, click the subitem of the listview to view the content of the subitem of the listview, and click the checkbox in the subitem to select or not the checkbox, the listview listener we are doing here is system. the position of the clicked listview.
Initialize data
data = new ArrayList<HashMap>();for (int i = 0; i < 20; i++) {map = new HashMap();map.put("title", "title-->" + i);map.put("content", "content--" + i);data.add(map);}
Set listview
Adapter = new myadapter (checklist. this, data); mlistview. setadapter (adapter); // Add a click event for the subitem of listview. position is the original arg2mlistview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {system. Out. println ("the row number you clicked is:" + position );}});
Define the adapter we need
int count = scroll_num;Context mContext;ArrayList<HashMap> mData;LayoutInflater mInflater;public MyAdapter(Context context, ArrayList<HashMap> data) {this.mContext = context;this.mData = data;mInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);if (count > mData.size()) {count = mData.size();}isSelected = new HashMap<Integer, Boolean>();for (int i = 0; i < data.size(); i++) {isSelected.put(i, false);}}@Overridepublic int getCount() {return mData.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {if (convertView == null) {convertView = mInflater.inflate(R.layout.item_listview, null);holder = new ViewHolder();holder.content = (TextView) convertView.findViewById(R.id.item_listview_content);holder.checkBox = (CheckBox) convertView.findViewById(R.id.item_listview_checkbox);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.content.setText(data.get(position).get("content").toString());holder.checkBox.setChecked(isSelected.get(position));holder.checkBox.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (isSelected.get(position)) {isSelected.put(position, false);} else {isSelected.put(position, true);}notifyDataSetChanged();}});return convertView;}
Finally, define an XML adapter.
Engineering Resources: http://download.csdn.net/detail/etzmico/3780822