Android listview + checkbox list sliding up or down causes loss of check box status solution, androidlistview

Source: Internet
Author: User

Android listview + checkbox list sliding up or down causes loss of check box status solution, androidlistview

I used to have a headache for this problem for a long time. I am busy with other things. I haven't sorted it out all the time. Today I am not so busy. Let's take a look at the problem of listview + checkbox.

Interface:



Listview_cell:


The interface is very simple. A full screen listview, cell is very simple, a textview and 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 the data. When it comes in, set every one to falsetempMap. put (I, false); // if it is not initialized, a null pointer will be reported. After all, if you have not saved anything, you will have to take it.} @ 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 (); convertView = 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 for the checkbox and save the status of the checkbox at 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 status of the current position}); // retrieves our status value from hashmap, then assign the value to the checkboxviewHolder at the corresponding position of the listview. cb_test.setChecked (tempMap. get (position); return convertView;} private ViewHolder viewHolder; public class ViewHolder {public TextView TV _title; public CheckBox cb_test ;}}



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.