以前为这个问题头疼很久.然后去忙其他事情.一直没有去整理,今天好不容易闲下来.就来整整这个listview + checkbox的问题吧
界面:
listview_cell:
界面很简单,一个全屏的listview,cell很简单,一个textview一个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; @Override protected 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++) {//初始化数据,刚进来的话把每一个都置为false tempMap.put(i, false); //如果不初始化会报空指针的,毕竟你还没存东西就要取了撒 } } @Override public int getCount() { return list==null?0:list.size(); } @Override public String getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public 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)); //为checkbox添加复选监听,把当前位置的checkbox的状态存进一个HashMap里面 viewHolder.cb_test.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { tempMap.put(position, isChecked);//把当前位置的状态给存储起来 } }); //从hashmap里面取出我们的状态值,然后赋值给listview对应位置的checkbox viewHolder.cb_test.setChecked(tempMap.get(position)); return convertView; } private ViewHolder viewHolder; public class ViewHolder{ public TextView tv_title; public CheckBox cb_test; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-13 00:39:18