开发者

Layout orientation changes without activity interaction

开发者 https://www.devze.com 2023-01-20 23:50 出处:网络
Since working with Android I\'m curious about how I can let Android do everything in respect to orientation change (layout vs. layout-land).

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[] {
                               开发者_如何学Python           R.id.activities_row_text1,
                                          R.id.activities_row_text2,
                                          R.id.activities_row_text3 });
    }
    if (simpleCursorAdapter != null) {
        setListAdapter(simpleCursorAdapter);
    }
}


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 Handling orientation changes yourself

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号