Java – ListActivity with checkbox in Android

androidcheckboxjavalistadapter

I am trying to use ListActivity and a SimpleCursorAdapter to check boxes based on a query from a database. The cursor is a list of questions and answers. If the user has already answered a question the checkbox should be checked but they aren't checked. The code looks like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.questions);


    Cursor c;
    testDbAdapter db = new testDbAdapter(this);
    c = db.getQuestions(Long.toString(mRowId), Integer.toString(mSection));
            startManagingCursor(c);

            String[] from = new String[]{testDbAdapter.QUESTIONS_Q, testDbAdapter.QUESTIONS_A};
            int[] to = new int[]{R.id.question, R.id.answer};

            SimpleCursorAdapter results = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, c, from, to);


            setListAdapter(results);
  }

Best Solution

You can extend the Adapter and override the bindView method, or call setViewBinder. A couple of detailed solutions to this are answered in a similar question here:

Android: Binding data from a database to a CheckBox in a ListView?