如何从 fragment 中清除ArrayList
并在适配器ListView
中执行notifyDataSetChanged()
?
这是我的快捷方式代码:
public class ConnectionsFragment {
public onClick() {
// do clear data and notify it (from ListView), how?!
}
private class ArrayAdapter extends BaseAdapter {
ArrayList<ConnectionsModel> data;
// constructor saves received data to `data`
// public view `getView()` displays it
}
}
我想要清除数据并在 fragment 类中的 onClick()
之后通知我已完成(或者如果更容易清除 ListView
直到我读取新数据)来自 API - 所以当我从 API 读取时我想要清晰的 ListView
)...
最佳答案
只需在适配器中创建一个公共(public)方法并将变量设置为私有(private),以防止对其进行不需要的编辑或清除:
private ArrayList<ConnectionsModel> mData;
public onClick() {
// do clear data and notify it (from ListView), how?!
clearData();
}
public void clearData() {
mData.clear();
// do something else here if you want. Like some kind of visual notification to the user
notifyDataSetChanged();
}
如果您的 onclick 方法位于创建适配器的 fragment 中(在本例中称为 mAdapter),则 onclick 将为:
public onClick() {
// do clear data and notify it (from ListView), how?!
mAdapter.clearData();
}
关于java - 清除ArrayList并在适配器ListView中从 fragment 执行notifyDataSetChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650720/