Question

I'm creating an Android app, where the user starts out seeing a list of questions. When the user clicks a question, I would like to show him a new clickable list, containing that question and its answer alternatives.

My questions reside in rows in the database. The questions live in the first column, while the answer alternatives live in the following columns.

I've managed to display any single element from the row when I click a question in the list, but how to display more than one completely eludes me.

This is the relevant source code in the intent entered when the user clicks a question:

private void fillData() {
    Bundle extras = getIntent().getExtras();
    Long id = extras != null ? extras.getLong(QuestionsDbAdapter.KEY_ROWID) : null;
    Cursor questionsCursor = mDbHelper.fetchQuestion(id);
    startManagingCursor(questionsCursor);
    String[] from = new String[]{mDbHelper.KEY_QUESTION,
        mDbHelper.KEY_ALT1,mDbHelper.KEY_ALT2};
    int[] to = new int[]{R.id.text1};
    SimpleCursorAdapter questions = 
        new SimpleCursorAdapter(this, R.layout.questions_one_row,
        questionsCursor, from,to);
    setListAdapter(questions);
}

This displays only the question. If I put one of the answer alternatives first in the 'String[] from ...' statement, that gets displayed instead.

Was it helpful?

Solution

This displays only the question.

That's because you told it to only display one thing:

int[] to = new int[]{R.id.text1};

If you want it to display more than one thing, you need more than one element in your to array. Ideally, the length of your from array and the length of your to array are identical. This, of course, means you also need additional TextView widgets in your res/layout/questions_one_row.xml file, whose android:id values go into the to array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top