ListView+CheckBox错乱问题


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);
			}
		});
	}

}












上一篇:解决ListView嵌套ListView遇到的问题


下一篇:Web 设计中的拉洋片效果 设计新潮