简体   繁体   English

屏幕旋转适配器notifyDataSetChanged不起作用

[英]Screen rotation adapter notifyDataSetChanged not working

I have a problem with updating RecyclerView when rotating the screen. 旋转屏幕时更新RecyclerView时出现问题。 By clicking on fab button I'm showing dialog fragment with input fields. 通过单击fab按钮,我将显示带有输入字段的对话框片段。 Then when I'm rotating the screen and saving data my RecyclerView is not updating. 然后,当我旋转屏幕并保存数据时,我的RecyclerView没有更新。 I'm setting adapter in onCreate method. 我在onCreate方法中设置适配器。 I should rotate once more in order to see fresh data. 我应该再旋转一次以查看新数据。 How can solve this issue. 如何解决这个问题。

Thanks in advance. 提前致谢。

dataSource = new MyBusinessDataSource(this);

        try
        {
            dataSource.openForRead();
        }
        catch (SQLException sqlEx)
        {
            Toast.makeText(MyBusinessMainActivity.this, R.string.sqlite_exception, Toast.LENGTH_SHORT).show();
            return;
        }

        categories = dataSource.getAllCategories();

        adapter = new CategoriesAdapter(this, categories);
        rv_list_cats.setAdapter(adapter);
        dataSource.close();

You need to override your onConfigurationChanged method in your activity or fragment and update your data there. 您需要在活动或片段中覆盖onConfigurationChanged方法并在那里更新数据。 Like. 喜欢。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(adapter!=null)
    {
        categories = dataSource.getAllCategories();
        adapter.notifyDataSetChanged();
    }
}

Also if you are not using the ScreenConfig property in your manifest use that looks like this. 另外,如果您未在清单中使用ScreenConfig属性,则使用如下所示。

   android:configChanges="orientation|screenSize"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM