如何使listview中的CheckBox被正确选中?

可以做一个数据model利用adapter和listview关联起来。

  1. package de.vogella.android.listactivity;
  2.     public class Model {

  3.     private String name;
  4.     private boolean selected;

  5.     public Model(String name) {
  6.         this.name = name;
  7.         selected = false;
  8.     }

  9.     public String getName() {
  10.         return name;
  11.     }

  12.     public void setName(String name) {
  13.         this.name = name;
  14.     }

  15.     public boolean isSelected() {
  16.         return selected;
  17.     }

  18.     public void setSelected(boolean selected) {
  19.         this.selected = selected;
  20.     }
  21. }
listview 的item layout

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" >

  5. <TextView
  6. android:id="@+id/label"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="@+id/label"
  10. android:textSize="30px" >
  11. </TextView>

  12. <CheckBox
  13. android:id="@+id/check"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentRight="true"
  17. android:layout_marginLeft="4px"
  18. android:layout_marginRight="10px" >
  19. </CheckBox>

  20. </RelativeLayout>
和listview关联的adapter

  1. import android.view.View;
  2.     import android.view.ViewGroup;
  3.     import android.widget.ArrayAdapter;
  4.     import android.widget.CheckBox;
  5.     import android.widget.CompoundButton;
  6.     import android.widget.TextView;

  7.     public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
  8.    
  9.         private final List<Model> list;
  10.         private final Activity context;
  11.    
  12.         public InteractiveArrayAdapter(Activity context, List<Model> list) {
  13.             super(context, R.layout.rowbuttonlayout, list);
  14.             this.context = context;
  15.             this.list = list;
  16.         }
  17.    
  18.         static class ViewHolder {
  19.             protected TextView text;
  20.             protected CheckBox checkbox;
  21.         }
  22.    
  23.         @Override
  24.         public View getView(int position, View convertView, ViewGroup parent) {
  25.             View view = null;
  26.             if (convertView == null) {
  27.                 LayoutInflater inflator = context.getLayoutInflater();
  28.                 view = inflator.inflate(R.layout.rowbuttonlayout, null);
  29.                 final ViewHolder viewHolder = new ViewHolder();
  30.                 viewHolder.text = (TextView) view.findViewById(R.id.label);
  31.                 viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
  32.                 viewHolder.checkbox
  33.                         .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  34.    
  35.                             @Override
  36.                             public void onCheckedChanged(CompoundButton buttonView,
  37.                                     boolean isChecked) {
  38.                                 Model element = (Model) viewHolder.checkbox
  39.                                         .getTag();
  40.                                 element.setSelected(buttonView.isChecked());
  41.    
  42.                             }
  43.                         });
  44.                 view.setTag(viewHolder);
  45.                 viewHolder.checkbox.setTag(list.get(position));
  46.             } else {
  47.                 view = convertView;
  48.                 ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
  49.             }
  50.             ViewHolder holder = (ViewHolder) view.getTag();
  51.             holder.text.setText(list.get(position).getName());
  52.             holder.checkbox.setChecked(list.get(position).isSelected());
  53.             return view;
  54.         }
  55. }
主activity

  1. package de.vogella.android.listactivity;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. import android.app.ListActivity;
  5. import android.os.Bundle;
  6. import android.widget.ArrayAdapter;

  7. public class MyList extends ListActivity {

  8.    
  9. /** Called when the activity is first created. */

  10.     public void onCreate(Bundle icicle) {
  11.         super.onCreate(icicle);
  12.         // Create an array of Strings, that will be put to our ListActivity
  13.         ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
  14.                 getModel());
  15.         setListAdapter(adapter);
  16.     }

  17.     private List<Model> getModel() {
  18.         List<Model> list = new ArrayList<Model>();
  19.         list.add(get("Linux"));
  20.         list.add(get("Windows7"));
  21.         list.add(get("Suse"));
  22.         list.add(get("Eclipse"));
  23.         list.add(get("Ubuntu"));
  24.         list.add(get("Solaris"));
  25.         list.add(get("Android"));
  26.         list.add(get("iPhone"));
  27.         // Initially select one of the items
  28.         list.get(1).setSelected(true);
  29.         return list;
  30.     }

  31.     private Model get(String s) {
  32.         return new Model(s);
  33.     }

  34. }

你可能感兴趣的:(如何使listview中的CheckBox被正确选中?)