论坛首页 移动开发技术论坛

ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去

浏览 10534 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-07-29  
Cindy_Lee   说的很对!!!
0 请登录后投票
   发表时间:2011-08-01  
View复用的问题。
0 请登录后投票
   发表时间:2011-08-02  
checkbox 的选中状态可以设置为数据的一个属性,每次检查这个属性,根据boolean值,设置checkbox是否选中,checkbox监听状态改变时,数据属性设置相应的值
0 请登录后投票
   发表时间:2011-12-16  
修改后的代码。
1. 去除重复代码
2. 改进性能,不需要在点击checkbox后遍历所有checkbox的状态了

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewCheckboxActivity extends Activity implements OnCheckedChangeListener {

	private ListView listView;
	private List<Map<String, Object>> list;
	private CheckBox cball;
	ListViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		listView = (ListView) findViewById(R.id.lv);
		//cball = (CheckBox) findViewById(R.id.cball);
		//cball.setOnCheckedChangeListener(this);//全选
listView.setOnItemClickListener(this);

		list = buildList();
		adapter = new ListViewAdapter(this, list);
		listView.setAdapter(adapter);
	}

	private List<Map<String, Object>> buildList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 20; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("icon", R.drawable.icon);
			map.put("title", "主题" + (i + 1));
			list.add(map);
		}
		return list;
	}

	class ListViewAdapter extends BaseAdapter {
		private Context context;
		private List<Map<String, Object>> list;
		private LayoutInflater inflater;
		<span style="background-color: #ff0000;">private List<Boolean> checkBoxesStatus;</span>

		
		public ListViewAdapter(Context context, List<Map<String, Object>> list) {
			super();
			this.context = context;
			this.list = list;
			int listsize = list.size();
			checkBoxesStatus = new ArrayList<Boolean>(listsize);
			for(int i = 0;i<list.size();i++){
				checkBoxesStatus.add(false);
			}
			this.inflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList() {
			return list;
		}
		public List<Boolean> getCheckBoxesStatus() {
			return checkBoxesStatus;
		}
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object 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) {
			
			ViewHolder viewHolder = null;
			Map<String, Object> map = list.get(position);
			Boolean checkBoxStatus = checkBoxesStatus.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
				viewHolder = new ViewHolder();
				viewHolder.img = (ImageView) convertView.findViewById(R.id.icon);
				viewHolder.tv = (TextView) convertView.findViewById(R.id.title);
				viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			viewHolder.img.setImageResource((Integer) map.get("icon"));
			viewHolder.tv.setText((String) map.get("title"));
			
			viewHolder.cb.setId(position);
			viewHolder.cb.setChecked(checkBoxStatus);
			//viewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			//	@Override
			//	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			//		checkBoxesStatus.set(buttonView.getId(), isChecked);
			//		notifyDataSetChanged();
			//	}
			//});
			return convertView;
		}
	}
	
	static class ViewHolder {
		private ImageView img;
		private TextView tv;
		private CheckBox cb;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		List<Boolean> checkBoxesStatus = adapter.getCheckBoxesStatus();
		//for(int i = 0;i<list.size();i++){
		//	checkBoxesStatus.set(i, isChecked);
		//}
		//adapter.notifyDataSetChanged();
                checkBoxesStatus.set(position, !checkBoxesStatus.get(position));
                adapter.notifyDataSetChanged();
	}
0 请登录后投票
   发表时间:2011-12-20   最后修改:2011-12-20

我是个初学的实习生,大四在读,不过在公司实习也遇到这个问题,这样解决的
http://mysfzj-web.iteye.com/blog/1317314
CB 保存在一个map里,key保存position,value保存是否选中,在适配器里 就可以通过position来找对应的cb并且赋值,达到这个效果。

 

public Map<Integer, Boolean> isSelected=new HashMap<Integer, Boolean>();
捕获事件处理,屏蔽了checkbox的事件:lv.setOnItemClickListener(new OnItemClickListener(){
                              @Override
                              public void onItemClick(AdapterView<?> parent, View view,
                              int position, long id) {
                              cb=(CheckBox) view.findViewById(R.id.cb);
                              //切换图片状态,选中或者没选中
                              cb.toggle();
                              //将状态存到map里保存
                              adapter.isSelected.put(position, cb.isChecked());
                        }});
在适配器的处理的方式            :if(isSelected!=null&&isSelected.get(position)!=null){
                             holder.cb.setChecked(isSelected.get(position));
                             }

 

      public View getView(final int position, View convertView, ViewGroup parent) {
            // 去掉下面那句,但是以后就每次会生成新的convertView
            //if(convertView==null)
            convertView=inflater.inflate(R.layout.a,null);

 

  boolean flag=false;
            holder.cb.setChecked(flag);
            if(isSelected!=null&&isSelected.get(position)!=null){
                  holder.cb.setChecked(isSelected.get(position));
            }

 

0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics