您可以尝试实施BookAdapter
作为单身人士来确认您没有打电话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);
当用户按 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);