本示例主要解决问题:
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>