Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // R.layout.adapters_checkboxcursor_row is just a simple layout file with a TextView and a CheckBox
- public class CheckBoxCursor extends ListActivity {
- private CustomAdapter adapter;
- // I used a MatrixCursor for easy testing
- private MatrixCursor mc;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.adapters_checkboxcursor);
- mc = new MatrixCursor(new String[] { "_id", "data" });
- for (int i = 1; i < 200; i++) {
- mc.addRow(new Object[] { i, "Item " + (i - 1) });
- }
- adapter = new CustomAdapter(this, R.layout.adapters_checkboxcursor_row,
- mc, new String[] { "data" }, new int[] { R.id.textView1 });
- setListAdapter(adapter);
- }
- public void doit(View v) {
- ArrayList<Boolean> ca = adapter.getItemsThatAreChecked();
- // this ArrayList will hold our Strings from the rows that were clicked.
- ArrayList<String> results = new ArrayList<String>();
- int s = ca.size();
- for (int i = 0; i < s; i++) {
- if (ca.get(i).booleanValue()) {
- mc.moveToPosition(i);
- Log.v("sss", mc.getString(mc.getColumnIndex("data")));
- results.add(mc.getString(mc.getColumnIndex("data")));
- }
- }
- }
- private class CustomAdapter extends SimpleCursorAdapter {
- private ArrayList<Boolean> checkItems = new ArrayList<Boolean>();
- public CustomAdapter(Context context, int layout, Cursor c,
- String[] from, int[] to) {
- super(context, layout, c, from, to);
- int count = c.getCount();
- for (int i = 0; i < count; i++) {
- checkItems.add(false);
- }
- }
- public ArrayList<Boolean> getItemsThatAreChecked() {
- return checkItems;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- super.bindView(view, context, cursor);
- int position = cursor.getPosition();
- CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox);
- ckb.setTag(new Integer(position));
- ckb.setChecked(checkItems.get(position));
- ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- Integer realPosition = (Integer) buttonView.getTag();
- checkItems.set(realPosition, isChecked);
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement