屏幕旋转后,Android的列表视图消失 [英] Android listview disappears after screen rotation

查看:179
本文介绍了屏幕旋转后,Android的列表视图消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On my layout, I have some buttons tho make some choices and then one button to perform a query to a database. The result of this query is shown in a ListView inside this layout.

现在的问题是,如果以后我执行查询我旋转屏幕,在的ListView 消失,不得不再次进行查询。

The problem is if after I perform the query I rotate the screen, the ListView disappears and have to perform the query again.

我认为,这种情况正在发生,因为该活动将重新启动。继建议,在这里 我已经添加到我的活动清单安卓:configChanges =方向| keyboardHidden在我的code补充说:

I believe that this is happening because the activity restarts. Following the suggestions here I've added to my activity in the manifest android:configChanges="orientation|keyboardHidden" and in my code added:

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

    }

不过,这是行不通的。

But this is not working.

下面是我的活动完整的code:

Here is the complete code of my activity:

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

    }
}

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

This is strange because I have some other activity like this:

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

    }

}   

这也执行数据库查询,其显示在的ListView ,然后处理用户的选择。如果我旋转屏幕,的ListView 仍显示。

This also performs a query to the database, shows it on a ListView and then handles the user choice. If I rotate the screen, ListView still shows.

我该怎么办?

推荐答案

当你这样做的setContentView在onConfigurationChanged方法ListView控件被重新创建。您需要将数据加载到的ListView了。如果你是在ListView绑定到一个适配器,你需要做的是在onConfigurationChanged。

The ListView gets recreated when you do setContentView in the onConfigurationChanged method. You need to load the data into the ListView again. if you are binding the ListView to an adapter, you need to do it in onConfigurationChanged.

这篇关于屏幕旋转后,Android的列表视图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆