屏幕旋转后,Android列表视图消失

Fav*_*las 9 android listview android-activity

在我的布局上,我有一些按钮做出一些选择,然后一个按钮来执行对数据库的查询.此查询的结果显示在ListView此布局中.

问题是如果在我执行查询后旋转屏幕,则ListView消失并且必须再次执行查询.

我相信这种情况正在发生,因为活动重新开始.按照这里的建议,我已经在清单android:configChanges="orientation|keyboardHidden"和我的代码中添加了我的活动:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.mylayout);

    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

这是我的活动的完整代码:

public class MyClass extends ListActivity implements OnClickListener, OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        // Creates the buttons and setOnClickListener and setOnCheckedChangeListener
    }

    @Override
    public void onClick(View v) {

        // Manages the buttons and their functions

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // See what group in radio group is checked
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // After pressing one button, a query is made and a listview is shown. 
        // This it to handle the user choice after he clicks an item on the listview

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.mylayout);

    }
}
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我还有其他一些活动:

public class AtoZ extends ListActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atoz);



    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

    }

}   
Run Code Online (Sandbox Code Playgroud)

这也会对数据库执行查询,在a上显示它ListView,然后处理用户选择.如果我旋转屏幕,ListView仍会显示.

我能做什么?

naa*_*eya 8

在清单文件下的活动中使用configChanges.

<activity
...
...
android:configChanges="orientation|screenSize|keyboard">
</activity
Run Code Online (Sandbox Code Playgroud)

这对我有用.

  • 只有这2个为我工作:orientation | screenSize (2认同)

Kel*_*inh 5

有两件事可以解决你的问题:

求解器A:

  1. 添加 android:configChanges="orientation|keyboardHidden"在您的清单XML文件.
  2. 删除 onConfigurationChanged(Configuration newConfig)功能.

因为onCreate()每次初始化Activity时只调用一次.当您使用空数据onConfigurationChanged()重新加载和膨胀新视图R.layout.mylayout时>您的列表不会与数据绑定.

求解器B:

已在onCreate()中编写的数据绑定代码移动覆盖方法onStart()onResume().我建议您使用onStart()数据绑定的情况.

您应该看到"开发"页面 Android Activity life cycle

(更新):

因为您listContent在单击列表项上的搜索按钮时加载,所以您必须维护活动中的数据以从中重建列表(例如:一串搜索).然后在onStart()您从此数据重建列表.

但是,单击List的项目以更改整个List本身时,您的逻辑很奇怪.并注意:onListItemClick(..)单击列表项上的按钮时也会触发.


Sam*_*eer 3

当您在 onConfigurationChanged 方法中执行 setContentView 时,将重新创建 ListView。您需要再次将数据加载到ListView中。如果要将 ListView 绑定到适配器,则需要在 onConfigurationChanged 中执行此操作。

  • 发现问题了。您需要将“|screenSize”添加到Manifest 中的configChanges 设置中。http://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting- Called (2认同)