配置更改后的notifyDataSetChanged

Otu*_*uyh 3 android android-adapter

我在更改设备配置(更改语言,方向等)后进行了一些测试,我注意到在此之后,方法"notifyDataSetChanged()"无法正常工作.

动作示例:

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

BookAdapter:

public void updateList(ArrayList<Book> books) {
     bookList = books;
     notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)

在配置更改后,我该怎么做才能使它工作?

编辑:

BookAdapter构造函数:

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

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;

    }

}
Run Code Online (Sandbox Code Playgroud)

Vik*_*ram 6

您可以尝试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);

        ....
        ....

}

}
Run Code Online (Sandbox Code Playgroud)

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

bookAdapter = BookAdapter.getInstance(getActivity(), listBook);
Run Code Online (Sandbox Code Playgroud)

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

// 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);
Run Code Online (Sandbox Code Playgroud)