android - 配置更改后 notifyDataSetChanged

标签 android android-adapter

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

Action 示例:

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

图书适配器:

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

配置更改后我该怎么做才能使其正常工作?

编辑:

BookAdapter 构造函数:

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

书籍 fragment :

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;

    }

}

最佳答案

您可以尝试将 BookAdapter 实现为 Singleton,以确认您没有从陈旧的引用调用 updateList(..)

您需要进行的更改:

// I am assuming that you are using a BaseAdapter because
// BookAdapter's constructor that you provided in the code above
// does not contain a call to super(....)
public class BookAdapter extends BaseAdapter {

    private static BookAdapter mAdapter;
    private Context context;
    private static ArrayList<Book> bookList;
    private BookDAO bookDAO;

    // To keep at most one instance of BookAdapter
    public static BookAdapter getInstance(Context con, ArrayList<Book> books) {

        // If an instance exists, return it
        if (mAdapter != null) {
            bookList = books;
            return mAdapter;
        }

        // Else, craete a new instance
        mAdapter =  new MyAdapter(con, books);
        return mAdapter;
    }

    // BookAdapter's only constructor is declared as private to restrict access
    private BookAdapter(Context con, ArrayList<Book> books) {
        context = con;
        bookList = books;
        bookDAO = BookDAO.getInstance(context);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Retrieve object
        Book bookItem = bookList.get(position);

        ....
        ....

}

}

这就是 Fragment 的 onCreateView 将如何改变:

bookAdapter = BookAdapter.getInstance(getActivity(), listBook);

当用户对 Are you sure you want to delete? 按 yes 时将执行的代码:

// Remove entry from bookDAO
// Remove entry from listBook
// OR update listBook:
try {
    listBook = bookDAO.getAll();
} catch (Exception e) {
    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}

// Assertion: "listBook" does not contain the 
// item that was just deleted from "bookDAO"

// Update ListView's contents
bookAdapter.updateList(listBook);

关于android - 配置更改后 notifyDataSetChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18773251/

相关文章:

android - 确保屏幕方向固定为纵向且永不更改

android - 为 ImageView 实现 GestureDetector

android - 数据库全局实例

java - 确定签名捕获笔划宽度的屏幕尺寸的最佳方法?

android - 在运行时将项目添加到 gridview 而不是替换以前的项目

java - 使用搜索 View 时适配器未更新

android - 在包含字符串名称和字符串子文本的 RecyclerView 中搜索字符串名称时出现问题

Android - notifyDataSetChanged() 和线程

android - 服务从 android OS 5.1.1 自动停止

android - 如何更新 RecyclerView - notifyDataSetChanged()