Android 的listview 内部item的布局包含checkbox控件时,会遇到常见的问题:1.checkbox无法选择;2.选择的checkbox后滑动listview内容时会checkbox选择的值会刷新成原来状态值(即没选中);3.选择listview所有checkbox值之后,只能获取当前可见的checkbox的值,向后滑动选择的值无法获取。
解决以上问题可以采取以下方法:
1.设置checkbox的属性值为:android:focusable=”false” ,防止焦点独占,解决checkbox无法选择的问题。
2.重写Adapter,重写getView方法;
3.重写Adapter的getView方法时,为每个checkbox添加事件响应并记录选择状态,通过获取获取状态记录值获取所有选择的checkbox值。
需要注意的是第三点,在重写getView方法时,不要判断convertView == null,如果判断convertView == null再实例化相关的控件,则刷新的是局部控件(这个尚需要观点有待论证)。
以下的程序代码就是围绕以上三点编写的例子。
图-1 获取listview checkbox 所有选择的值
ListView_CheckBoxActivity.Java实现界面及其事件功能,通过点击Button按钮获取选择的checkbox值,并在Toast控件中显示,如图-1。
1. ListView_CheckBoxActivity.java
- package com.checkbox.main;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.Toast;
- public class ListView_CheckBoxActivity extends Activity {
- //适配器
- CheckboxAdapter listItemAdapter;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //按钮及事件响应
- Button getValue=(Button)findViewById(R.id.get_value);
- getValue.setOnClickListener(listener);
- //listview
- ListView list = (ListView) findViewById(R.id.list);
- //存储数据的数组列表
- ArrayList<HashMap<String, Object>> listData=new ArrayList<HashMap<String,Object>>();
- String []name={"William","Charles","Linng","Json","Bob","Carli","William","Charles","Linng",
- "Json","Bob","Carli"};
- String []id={"1","2","3","4","5","6","7","8","9","10","11","12"};
- for(int i=0;i<12;i++){
- HashMap<String, Object> map=new HashMap<String, Object>();
- map.put("friend_image", R.drawable.icon);
- map.put("friend_username", name[i]);
- map.put("friend_id", id[i]);
- map.put("selected", false);
- //添加数据
- listData.add(map);
- }
- //适配器
- listItemAdapter = new CheckboxAdapter(this, listData);
- list.setAdapter(listItemAdapter);
- }
- //事件响应
- OnClickListener listener=new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- HashMap<Integer, Boolean> state =listItemAdapter.state;
- String options="选择的项是:";
- for(int j=0;j<listItemAdapter.getCount();j++){
- System.out.println("state.get("+j+")=="+state.get(j));
- if(state.get(j)!=null){
- @SuppressWarnings("unchecked")
- HashMap<String, Object> map=(HashMap<String, Object>) listItemAdapter.getItem(j);
- String username=map.get("friend_username").toString();
- String id=map.get("friend_id").toString();
- options+="\n"+id+"."+username;
- }
- }
- //显示选择内容
- Toast.makeText(getApplicationContext(), options, Toast.LENGTH_LONG).show();
- }
- };
- }
CheckboxAdapter 继承BaseAdapter并重写相关函数,重写getView时,为每个checkbox添加事件响应并通过HashMap记录选择状态,以便获取相应的checkbox值。
2.CheckboxAdapter .java
- package com.checkbox.main;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class CheckboxAdapter extends BaseAdapter {
- Context context;
- ArrayList<HashMap<String, Object>> listData;
- //记录checkbox的状态
- HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();
- //构造函数
- public CheckboxAdapter(Context context,ArrayList<HashMap<String,Object>> listData) {
- this.context = context;
- this.listData = listData;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return listData.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return listData.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- // 重写View
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- LayoutInflater mInflater = LayoutInflater.from(context);
- convertView = mInflater.inflate(R.layout.item, null);
- ImageView image = (ImageView) convertView.findViewById(R.id.friend_image);
- image.setBackgroundResource((Integer) listData.get(position).get("friend_image"));
- TextView username = (TextView) convertView.findViewById(R.id.friend_username);
- username.setText((String) listData.get(position).get("friend_username"));
- TextView id = (TextView) convertView.findViewById(R.id.friend_id);
- id.setText((String) listData.get(position).get("friend_id"));
- CheckBox check = (CheckBox) convertView.findViewById(R.id.selected);
- check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if (isChecked) {
- state.put(position, isChecked);
- } else {
- state.remove(position);
- }
- }
- });
- check.setChecked((state.get(position) == null ? false : true));
- return convertView;
- }
- }
加载的布局文件
3.main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <Button android:id="@+id/get_value"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="获取Checkbox值" />
- <ListView android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
listview item内部的布局文件
4.item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RelativeLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip"
- android:paddingRight="12dip" >
- <ImageView android:id="@+id/friend_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="6dip"
- android:paddingLeft="2dip"
- android:layout_centerVertical="true"
- android:layout_alignParentLeft="true" />
- <TextView android:id="@+id/friend_username"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="18dip"
- android:textColor="#ccc"
- android:paddingTop="6dip"
- android:paddingRight="2dip"
- android:layout_toRightOf="@id/friend_image" />
- <TextView android:id="@+id/friend_id"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/friend_username"
- android:layout_marginRight="36dip"
- android:paddingRight="2dip"
- android:layout_toRightOf="@id/friend_image"
- android:textColor="#fff"
- android:maxLines="2" />
- <CheckBox android:id="@+id/selected"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="36dip"
- android:layout_centerVertical="true"
- android:layout_alignParentRight="true"
- android:focusable="false" />
- </RelativeLayout>