仿米聊添加好友 listview中checkbox选中,更新选择条数问题
来源:互联网 发布:java的compareto方法 编辑:程序博客网 时间:2024/04/30 13:41
本示例主要解决问题:
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. */@Overridepublic 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 {@Overridepublic 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);}}@Overridepublic int getCount() {return arrayList.size();}@Overridepublic Object getItem(int position) {return arrayList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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 {@Overridepublic 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>
- 仿米聊添加好友 listview中checkbox选中,更新选择条数问题
- 关于listview中checkbox的选中问题
- listview中使用checkbox选中问题
- listView中添加checkBox按键,点击是否选中处理。
- 关于ListView中checkBox选中状态错乱问题的解决办法
- android中ListView添加CheckBox控件实现选择
- javascript判断checkbox选中条数
- WPF ListBoxItem模板中添加CheckBox选中问题
- xmpp 如何添加全部好友(使用了listview + checkbox)技术
- ListView中有Checkbox时的点击和选择问题
- 关于ListView中CheckBox选择混乱的问题
- ListView checkbox 重复选中的问题。
- ListView+CheckBox 选中状态错乱问题
- listView中有checkBox选中错乱问题解决
- ListView中获取checkbox选中事件
- 解决 listview 中添加 checkbox 出现的问题
- ListView中带有CheckBox时,解决CheckBox重复选择问题的一个不错方案
- listView的item中有checkbox,重复被选中的问题
- 僵尸进程
- TCP/IP 第1章 概述
- 浅谈android的selector背景选择器
- 转载和积累系列 - 微博计数器
- 集合之集合原理(一)
- 仿米聊添加好友 listview中checkbox选中,更新选择条数问题
- Hdu 1709 The Balance
- TCP/IP 第2章 链路层
- 集合之集合使用(二)
- 进程死锁及解决办法
- vs2008 <pages theme="theme1">
- TCP/IP 第3章 IP: 网际协议
- TWaver局部自动布局及嵌套Group处理
- TCP/IP 第4章 ARP:地址解析协议