gpt4 book ai didi

android - 配置更改后 notifyDataSetChanged

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:54 24 4
gpt4 key购买 nike

更改设备配置(更改语言、方向等)后我正在做一些测试,我注意到在此之后,方法“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com