Advertisement
Guest User

fghgfjhfg

a guest
May 8th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. // R.layout.adapters_checkboxcursor_row is just a simple layout file with a TextView and a CheckBox
  2. public class CheckBoxCursor extends ListActivity {
  3.  
  4.     private CustomAdapter adapter;
  5.         // I used a MatrixCursor for easy testing  
  6.     private MatrixCursor mc;
  7.  
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.adapters_checkboxcursor);
  12.         mc = new MatrixCursor(new String[] { "_id", "data" });
  13.         for (int i = 1; i < 200; i++) {
  14.             mc.addRow(new Object[] { i, "Item " + (i - 1) });
  15.         }
  16.         adapter = new CustomAdapter(this, R.layout.adapters_checkboxcursor_row,
  17.                 mc, new String[] { "data" }, new int[] { R.id.textView1 });
  18.         setListAdapter(adapter);
  19.     }
  20.  
  21.     public void doit(View v) {
  22.         ArrayList<Boolean> ca = adapter.getItemsThatAreChecked();
  23.         // this ArrayList will hold our Strings from the rows that were clicked.
  24.         ArrayList<String> results = new ArrayList<String>();
  25.         int s = ca.size();
  26.         for (int i = 0; i < s; i++) {
  27.             if (ca.get(i).booleanValue()) {
  28.                 mc.moveToPosition(i);
  29.                 Log.v("sss", mc.getString(mc.getColumnIndex("data")));
  30.                 results.add(mc.getString(mc.getColumnIndex("data")));
  31.             }
  32.         }
  33.     }
  34.  
  35.     private class CustomAdapter extends SimpleCursorAdapter {
  36.  
  37.         private ArrayList<Boolean> checkItems = new ArrayList<Boolean>();
  38.  
  39.         public CustomAdapter(Context context, int layout, Cursor c,
  40.                 String[] from, int[] to) {
  41.             super(context, layout, c, from, to);
  42.             int count = c.getCount();
  43.             for (int i = 0; i < count; i++) {
  44.                 checkItems.add(false);
  45.             }
  46.  
  47.         }
  48.  
  49.         public ArrayList<Boolean> getItemsThatAreChecked() {
  50.             return checkItems;
  51.         }
  52.  
  53.         @Override
  54.         public void bindView(View view, Context context, Cursor cursor) {
  55.             super.bindView(view, context, cursor);
  56.             int position = cursor.getPosition();
  57.             CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox);
  58.             ckb.setTag(new Integer(position));
  59.             ckb.setChecked(checkItems.get(position));
  60.             ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  61.  
  62.                 @Override
  63.                 public void onCheckedChanged(CompoundButton buttonView,
  64.                         boolean isChecked) {
  65.                     Integer realPosition = (Integer) buttonView.getTag();
  66.                     checkItems.set(realPosition, isChecked);
  67.                 }
  68.  
  69.             });
  70.         }
  71.  
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement