配置更改后notifyDataSetChanged [英] notifyDataSetChanged after configuration change

查看:88
本文介绍了配置更改后notifyDataSetChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改设备配置(更改语言,方向等)后,我正在做一些测试,我注意到在此之后,方法"notifyDataSetChanged()"不起作用.

I'm doing some test after change device configuration (change language, orientation, etc), and i notice that after this, the method "notifyDataSetChanged()" is not working.

操作示例:

我每次执行删除,保存等操作时,都会调用updateList().当我更改方向时,用户单击删除按钮,将显示一个DialogFragment,您确定要删除吗?". ,语言或设备的任何配置,然后在对话框上单击是",数据将被删除,但列表不会更新.我需要退出活动,然后返回以查看更改.

I'm calling updateList() everytime i do an action like delete, save, etc. The user click a delete button, a DialogFragment is shown, "Are you sure you want to delete?", when i change the orientation, or the language, or any configuration of the device and then click "yes" on the Dialog, the data is removed, but the list doesn't update. I need to quit the activity, then go back to see the alteration.

BookAdapter:

public void updateList(ArrayList<Book> books) {
     bookList = books;
     notifyDataSetChanged();
}

更改配置后如何使它工作?

What can i do to make it works after the configuration change?

BookAdapter构造函数:

BookAdapter Constructor:

public BookAdapter(Context c, ArrayList<Book> books) {
    context = c;
    bookList = books
    bookDAO = BookDAO.getInstance(context);
}

BookFragment:

BookFragment:

public class BookFragment extends Fragment {

    private BookDAO bookDAO;

    private BookAdapter bookAdapter;

    private ListView listBook;

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        bookDAO = bookDAO.getInstance(getActivity());

        view = inflater.inflate(R.layout.book_tab, container, false);

        ArrayList<Book> listBook = null;

        try {
            llistBook = bookDAO.getAll();
        } catch (Exception e) {
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
            return view;
        }

        bookAdapter = new BookAdapter(getActivity(), listBook);
        listBook = (ListView)view.findViewById(R.id.listBook);
        listBook.setAdapter(bookAdapter);

        return view;

    }

}

推荐答案

之所以会出现此问题,是因为每次旋转,更改语言等...都会重新创建活动,并且还会重新创建片段(新实例),因此notififyDataSetChanged实际上是在通知片段的旧实例. 一个解决方案将是.使您的片段静止.然后,为片段创建一些刷新方法,并在对话框中按是"时调用它.

The problem occurs because every time your rotate, change language, etc... the activity is recreated and your fragments are also recreated (new instance), so the notififyDataSetChanged is actually notifying the old instances of your fragments. A solution for that would be. Make your fragments static. Then you create some refresh method for your fragments, and called it when you press yes for your dialog.

在活动中,您应该有类似的内容.

In your activity you should have something like this.

private static BookFragment  bookFragment;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    if (fragment1 == null) {
        bookFragment = new BookFragment();
    }

    ...
}

创建一些界面,例如:

public interface Refreshable {
    public void refresh();
}

然后实现您所有片段的此接口. 在肯定回答对话框的方法中,您应该调用

Then implement this interface all your fragments. In the method that is called when the dialog is answered positively you should call

...
fragment.refresh();
...

在片段的refresh方法中,可以调用其适配器方法updateList(...)

Inside the refresh method of you fragment you can call its adapter method updateList(...)

可能不是更漂亮的解决方案,但是它可以工作....

Might not be the prettier solution, but it works....

为什么会这样?Google的Android开发团队可能知道.

Why this happen... Google's Android Dev Team might know.

这篇关于配置更改后notifyDataSetChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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