ListView+CheckBox错乱问题

简介: public class MyAdapter extends BaseAdapter { private Context mContext; private ArrayList<Map<String, Object>> data; public static HashMap<Integer, Boolean> mapisSelector;


public class MyAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Map<String, Object>> data;
	public static HashMap<Integer, Boolean> mapisSelector;

	public MyAdapter(Context context, ArrayList<Map<String, Object>> data) {
		this.mContext = context;
		this.data = data;
		initIsCheck();
	}
	
	//初始化checkbox的勾选状态都是 未勾选
	void initIsCheck(){
		if(null == mapisSelector){
			mapisSelector = new HashMap<Integer, Boolean>();
			for (int i = 0; i < 50; i++) {
				mapisSelector.put(i, false);
			}
		}
	}
	

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Map<String, Object> getItem(int position) {

		return data.get(position);
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.custom_list_item,
					null);
			viewHolder = new ViewHolder();
			// 获取控件
			viewHolder.tvName = (TextView) convertView
					.findViewById(R.id.tv_qq_name);
			// 获取控件
			viewHolder.imgQQ = (ImageView) convertView
					.findViewById(R.id.img_qq);
			// 获取控件
			viewHolder.cbQQ = (CheckBox) convertView.findViewById(R.id.cb_qq);
			// 绑定到返回的视图里
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 赋值
		viewHolder.tvName.setText(data.get(position).get("txt").toString());
		// 赋值
		viewHolder.imgQQ.setImageResource((Integer) data.get(position).get(
				"img"));
		
		viewHolder.cbQQ.setChecked(mapisSelector.get(position));//设置选中状态
		viewHolder.cbQQ.setTag(position);//设置tag、position
		viewHolder.cbQQ.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer in = (Integer) v.getTag();//得到当前tag
				if (mapisSelector.get(in)) {
					mapisSelector.put(in, false);//改为未选中
				}else{
					mapisSelector.put(in, true);//改为选中
				}
			}
		});
		
//		// 赋值
//		viewHolder.cbQQ.setChecked(true);
		return convertView;
	}
	
	
	public void resetDataSource(ArrayList<Map<String, Object>> data1){
		
		data.addAll(data1);
		this.notifyDataSetChanged();
	}

	static class ViewHolder{
		TextView tvName;
		ImageView imgQQ;
		CheckBox cbQQ;
	}
}


public class MainActivity extends Activity {

	private ListView mListView;
	private ArrayList<String> listData;
	private ArrayAdapter<String> listAdapter;

	private ArrayList<Map<String, Object>> sAdapterData;
	private SimpleAdapter sAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取控件
		mListView = (ListView) this.findViewById(R.id.listview);
//		initListView();
		 initSadpter();
	}

	private void initListView() {

		// 1、初始化数据源
		listData = new ArrayList<String>();
		for (int i = 0; i < 50; i++) {
			listData.add("ListView测试文本,第" + (i + 1) + "项");
		}

		// 2、将数据源添加到适配器上,形成资源列表
		listAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, listData);

		// 3、将适配器资源列表添加到适配器控件上
		mListView.setAdapter(listAdapter);
	}

	private void initSadpter() {

		// 1、初始化数据源
		sAdapterData = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (int i = 0; i < 50; i++) {
			map = new HashMap<String, Object>();
			if (i % 2 == 0) {
				map.put("img", R.drawable.qq);
			} else {
				map.put("img", R.drawable.qq_kong);
			}
			map.put("txt", "小黑");
			map.put("cb", " ");
			sAdapterData.add(map);
		}
		String[] from = { "img", "txt", "cb" };
		int[] to = { R.id.img_qq, R.id.tv_qq_name, R.id.cb_qq };
		// 2、将数据源添加到适配器上,形成资源列表
		sAdapter = new SimpleAdapter(this, sAdapterData, R.layout.simple_main,
				from, to);
		// 3、将适配器资源列表添加到适配器控件上
		mListView.setAdapter(sAdapter);

		// 点击Items时执行的监听器
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, Object> mapxx=(Map<String, Object>) sAdapter.getItem(position);
				String name=mapxx.get("txt").toString();
				
				
				Log.e("", "------onItemClick--------");
				startActivity(new Intent(MainActivity.this, CustomAdapterActivity.class));
			}
		});

		// 长按Item时执行
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.e("", "------onItemLongClick--------");
				startActivity(new Intent(MainActivity.this, CustomAdapterActivity.class));

				return false;
			}
		});
	}
}


public class CustomAdapterActivity extends Activity {

	private ListView mListView;
	private ArrayList<Map<String, Object>> sAdapterData;
	private MyAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_main);
		// 获取控件
		mListView = (ListView) this.findViewById(R.id.mylist);

		initDataSource();
		initAdapter();

	}

	/**
	 * 1、初始化数据源
	 */
	private void initDataSource() {

		sAdapterData = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = null;
		for (int i = 0; i < 50; i++) {
			map = new HashMap<String, Object>();
			if (i % 2 == 0) {
				map.put("img", R.drawable.qq);
			} else {
				map.put("img", R.drawable.qq_kong);
			}
			map.put("txt", "小黑");
			map.put("cb", " ");
			sAdapterData.add(map);
		}
	}

	private void initAdapter() {
		myAdapter = new MyAdapter(CustomAdapterActivity.this, sAdapterData);
		mListView.setAdapter(myAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				myAdapter.getItem(position);
			}
		});
	}

}












目录
相关文章
|
7月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
727 1
|
7月前
|
Android开发
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
|
API Android开发
ListView或GridView上添加能一起滚动的Button效果的解决思路
ListView或GridView上添加能一起滚动的Button效果的解决思路
Dialog和软键盘的冲突
一、冲突描述 为了说明问题,我先随意在我之前的项目写个dialog做个测试。 先写一个dialog,自定义的,宽高为填充屏幕,里面是两个edittext。
2114 0
ListView 中Checkbox 错乱问题
最近项目需求,模仿今日头条的短视频播放,点击其中一个视频,将其他的视频停止播放。撸完代码后执行,发现会发现图片错位的情况,然后去谷歌,寻求解决,大多数都是setTag()方式,可以试过后还是存在问题。
746 0
|
Android开发
Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
<div class="markdown_views"> <h1 id="android高级控件一listview绑定checkbox实现全选增加和删除等功能">Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能</h1> <hr> <blockquote> <p>这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一
2114 0
ListView单选的实现总结(转)
          今天在智能停车场项目中需要实现PullToRefreshListView的单选功能,考虑到分页,刷新等,以前的实现方式是采用自己维护一个集合保存选中位置的选中状态,但这个方式比较繁琐,今天采用了listview的单选模式     listView.
896 0