views:

31

answers:

1

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.

A: 

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.

CommonsWare
Thanks! Yes, that would display them all, but in the same element, so they are not individually clickable. I can only click the whole thing, questions, answers and all. I prefer them to be in a list, as separate items.
Tom Hagen
@Tom Hagen: Oh. In that case, you will need to eliminate much of your current code and replace it. You will need to invent your own adapter (extending BaseAdapter) that handles your scenario, correctly computing the count of rows. You can use getItemTypeCount() and getItemViewType() to format the questions and answers differently, if needed. And so on.
CommonsWare
I'm a complete newb to both Android and Java, but I have a very strong feeling there should be a one-liner somewhere that could just pivot the rows into a column, that could then be sent to setListAdapter.
Tom Hagen
@Tom Hagen: No, sorry.
CommonsWare
I ended up using string arrays instead, since for some unapparent reason it's apparently impossible, like you say, to display a whole row from an android database.
Tom Hagen
@Tom Hagen: not impossible, just not a one-liner. Off the cuff, I'd expect it to take 30-40 lines.
CommonsWare
He he :) In the languages I'm used to, so much code for something that simple =:= impossible.
Tom Hagen