How can I clear ArrayList
and do notifyDataSetChanged()
in adapter ListView
from fragment?
This is my code in a shortcut:
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
}
}
And I want to do clear data and notify I done it after onClick()
in fragment class (or if it is even easier clear ListView
until I read new data from API - so I want clear ListView
when I do reading from API)...
答案 0 :(得分:3)
just create a public method in the adapter and make the variable private to protect it against unwanted edits or clearing:
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();
}
if your onclick method is in the fragment that's creating the adapter (call it mAdapter in this example), then the onclick would be:
public onClick() {
// do clear data and notify it (from ListView), how?!
mAdapter.clearData();
}
答案 1 :(得分:0)
If your getView()
method is pulling from the data
ArrayList, you should be able to call data.clear();
and then notifyDataSetChanged();
from within your ArrayAdapter
class