布局方向发生变化而无需 Activity 交互

发布于 2024-09-29 03:02:48 字数 1969 浏览 3 评论 0原文

自从使用 Android 以来,我很好奇如何让 Android 完成有关方向更改(布局与布局区域)的所有操作。

目前,我必须将正确数量的数据库列和视图提供给游标适配器。这是正确的方法还是我错过了什么?你们是怎么做到的?

请看一下两个 SimpleCursorAdapter,我在其中提供了两个现有布局的相同布局名称(布局中一个,布局域中一个)。唯一的区别是附加的数据库列“type”和附加视图“R.id.activities_row_text3”。

这是正确的方法吗?

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);
    }
}

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-10-06 03:02:48

最好在启动 Activity 时仅创建一次 simpleCursorAdapter。然后,当方向发生变化时,您可以使用方法 SimpleCursorAdapter.changeCursorAndColumns() (或 bindView())。您可能需要使列表无效才能看到更改 (notifyDataSetInvalidated())。

为了避免在发生方向更改时重新启动 Activity,请参阅自行处理方向更改

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文