前面的已经介绍了如何将check选中,但是很不尽如人意的是,每次listView都会调用getView方法来刷新页面,所以导致之前选中的check会在调用getView之后会被取消。
通过:
http://blog.csdn.net/whucyl/article/details/7341133 上面的提示对部件进行了重写。
使用一个select的方法,监听选中是否,然后加入一个hashmap,用来保存每次用户选中的状态,每次getView的查看此状态即可。
一下是代码:
MyAdapter方法:
- package com.duduli.li;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.database.Cursor;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.CheckedTextView;
- import android.widget.SimpleCursorAdapter;
- import android.widget.TextView;
- public class MyAdapter extends SimpleCursorAdapter{
- private List list = new ArrayList();
- private HashMap isSelected = null;
- public String s[];
- private List
- private LayoutInflater layoutInflater;
- private List delNumber = new ArrayList();
- @SuppressWarnings("unused")
- private Context context;
- public MyAdapter(Context context, int layout, Cursor c,
- String[] from, int[] to) {
- super(context, layout, c, from, to);
- this.context = context;
- this.data = getData();
- this.layoutInflater = LayoutInflater.from(context);
- isSelected = new HashMap();
- }
- @Override
- public int getCount() {
- return super.getCount();
- }
- @Override
- public Object getItem(int position) {
- return super.getItem(position);
- }
- @Override
- public long getItemId(int position) {
- return super.getItemId(position);
- }
- @SuppressWarnings("unused")
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- Compan cp = null;
- if(cp == null){
- cp = new Compan();
- convertView = layoutInflater.inflate(R.layout.list, null);
- cp.id = (TextView) convertView.findViewById(R.id._id);
- cp.ctv = (CheckedTextView) convertView.findViewById(R.id.checkText);
- convertView.setTag(cp);
- }else{
- cp = (Compan) convertView.getTag();
- }
- cp.id.setText((CharSequence) data.get(position).get("_id"));
- cp.ctv.setText((CharSequence) data.get(position).get("Number"));
- Boolean b = isSelected.get(position);
- if(null == b){
- b = false;
- }
- if (convertView instanceof ViewGroup) {
- ViewGroup g = (ViewGroup) convertView;
- for (int i = 0; i < g.getChildCount(); i++) {
- View v = g.getChildAt(i);
- if (v instanceof CheckedTextView) {
- ((CheckedTextView) v).setChecked(b);
- break;
- }
- }
- }
- return convertView;
- }
- public List
- Listnew ArrayList
- while (getCursor().moveToNext()) {
- Map map = new HashMap();
- map.put("_id", getCursor().getString(0));
- map.put("Number", getCursor().getString(1));
- list.add(map);
- }
- return list;
- }
- public void select(int postion, boolean isChecked) {
- isSelected.put(postion, isChecked);
- }
- }
package com.duduli.li;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MyAdapter extends SimpleCursorAdapter{
private List list = new ArrayList();
private HashMap
isSelected = null;
public String s[];
private List
阅读(682) | 评论(0) | 转发(0) |