views:

31

answers:

1

Since working with Android I'm curious about how I can let Android do everything in respect to orientation change (layout vs. layout-land).

Currently I have to feed the correct number of db columns and views to the cursor adapter. Is this the correct way or do I miss something? How do you guys do that?

Please have a look at the two SimpleCursorAdapter where I feed the same layout name of both existing layouts (there's on in layout and one in layout-land). The only difference is the additional db column "type" and the additional view "R.id.activities_row_text3".

Is this the correct way?

Cursor cursor;
SimpleCursorAdapter simpleCursorAdapter = null;
if ((cursor = db.fetchActivities(connection)) != null) {
    startManagingCursor(cursor);
    int orientation = getResources().getConfiguration().orientation; 
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        simpleCursorAdapter = new SimpleCursorAdapter(
                                      this,
                                      R.layout.activities_row,
                                      cursor,
                                      new String[] {
                                          "name",
                                          "time" },
                                      new int[] {
                                          R.id.activities_row_text1,
                                          R.id.activities_row_text2 });
    } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        simpleCursorAdapter = new SimpleCursorAdapter(
                                      this,
                                      R.layout.activities_row,
                                      cursor,
                                      new String[] {
                                          "name",
                                          "time",
                                          "type" },
                                      new int[] {
                                          R.id.activities_row_text1,
                                          R.id.activities_row_text2,
                                          R.id.activities_row_text3 });
    }
    if (simpleCursorAdapter != null) {
        setListAdapter(simpleCursorAdapter);
    }
}

Many thanks in advance. HjW

A: 

It would be better to create simpleCursorAdapter only once, while starting Activity. Then you can use method SimpleCursorAdapter.changeCursorAndColumns() (or bindView()) when change orientation occurs. You may need to invalidate list to see changes (notifyDataSetInvalidated()).

To avoid restarting activity when orientation change occurs, see http://stackoverflow.com/questions/2967903/handling-orientation-changes-yourself

tomash
Thanks for your answer. The code shown above is part of every activities onCreate because my apps usually support different layouts. I came to the conclusion that I must be doing something terribly wrong. With different screen sizes and orientations the list of different cursor/adapter/view combinations did increase and I thought "ask the pros". Consider as shown two layouts: One with 2 columns and one with three columns (just a test - it's usually more complicated). Do you use two different cursor or one cursor fetching all? How/where do you decide what columns/views to use? Thanks hjw
hjw
Use one cursor for one kind of query (wrap Cursor in own class and provide convenient constructors). Write own adapter class instead of using SimpleAdapters - looks compilcated at the beginning, but it becomes easy quickly. Reuse those classes in multiple activities. You can pre-define layouts or change layout properies (like column number) in the runtime, both solutions have pros and cons. Don't mix UI with data structures - you will be able to improve code later, after learning new techniques.
tomash