仿米聊添加好友 listview中checkbox选中,更新选择条数问题

本示例主要解决问题:

1、listview中checkbox多选中错乱问题

2、实时更新选中的条数

效果图:


代码示例:

Activity:

public class CountDemoActivity extends Activity {
	protected static final String TAG = "CountDemoActivity";
	private ListView listView;
	private ArrayList<HashMap<String, String>> arrayList;
	private ArrayList<Boolean> checkedList;
	private MyOnCheckedChangeListener checkedChangeListener = new MyOnCheckedChangeListener();
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int count = 0;
			for (int i = 0; i < checkedList.size(); i++) {
				boolean isChecked = (boolean) checkedList.get(i);
				if (isChecked) {
					count++;
				}
			}
			selectButton.setText("选择(" + String.valueOf(count) + ")");
		};
	};
	private Button selectButton;
	private MyListViewAdapter myListViewAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		selectButton = (Button) findViewById(R.id.select);

		arrayList = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < 20; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("content", "Content" + i);
			arrayList.add(map);
		}

		listView = (ListView) findViewById(R.id.listView);
		listView.setOnItemClickListener(new ListViewItemOnClick());
		myListViewAdapter = new MyListViewAdapter(arrayList);
		listView.setAdapter(myListViewAdapter);

	}

	private class ListViewItemOnClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast.makeText(CountDemoActivity.this, String.valueOf(position), 0)
					.show();
		}

	}

	private class MyListViewAdapter extends BaseAdapter {

		private int listsize;

		public MyListViewAdapter(ArrayList<HashMap<String, String>> arrayList) {
			this.listsize = arrayList.size();
			checkedList = new ArrayList<Boolean>(listsize);
			for (int i = 0; i < arrayList.size(); i++) {
				checkedList.add(false);
			}
		}

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

		@Override
		public Object getItem(int position) {
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				LayoutInflater inflater = LayoutInflater
						.from(CountDemoActivity.this);
				convertView = inflater.inflate(R.layout.listview_item, null);
				holder = new ViewHolder();
				holder.content = (TextView) convertView
						.findViewById(R.id.content);
				holder.cbox = (CheckBox) convertView
						.findViewById(R.id.checkBox);

				holder.cbox.setOnCheckedChangeListener(checkedChangeListener);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Log.e(TAG, "getView " + String.valueOf(position));
			holder.content.setText(arrayList.get(position).get("content"));
			holder.cbox.setId(position);
			if (!checkedList.isEmpty()) {
				boolean isChecked = (Boolean) checkedList.get(position);
				holder.cbox.setChecked(isChecked);
			}

			return convertView;
		}

		class ViewHolder {
			CheckBox cbox;
			TextView content;
		}

	}

	private class MyOnCheckedChangeListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			Message message = new Message();
			Bundle bundle = new Bundle();
			bundle.putBoolean("isChecked", isChecked);
			message.setData(bundle);
			handler.sendMessage(message);

			checkedList.set(buttonView.getId(), isChecked);
			Log.e("isChecked", String.valueOf(isChecked));
		}

	}

}

layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="选择" />
	<LinearLayout 
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >
        <Button 
            android:id="@+id/select"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="选择(0)"
            android:layout_weight="1"
            />
        <Button 
            android:id="@+id/cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="取消"
            android:layout_weight="1"
            />
    </LinearLayout>
    <ListView 
        android:id="@+id/listView"
        android:layout_below="@+id/title"
        android:layout_above="@id/linear"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        ></ListView>
    
</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值