The typical usage of notifyDataSetChanged()
is to call it in the same class that handles your views.
For example, in your Fragment's
onViewCreated()
, you get a reference to the ListView
and the adapter, and when you have your data ready, typically in the onPostExecute()
you would call notifyDataSetChanged()
to update the view.
If you prefer to pass your adapter to another object to do the data manipulation, you can just go ahead and do it, nothing special since java passes object by reference, so your Adapter object will be the same object in both classes
Edit:
My assumptions are:
- your Adapter is called
sta
- your Fragment is called
FragmentSavedTrips
- your object that modifies the data is call
SavedTripsChanger
- your data string array is called from.
Inside SavedTripsChanger
have a private variable to hold the adapter
private SavedTripsAdapter mSavedTripsAdapter;
and a setter method (optional getter method)
public SavedTripsAdapter setSavedTripsAdapter(SavedTripsAdapter mSavedTripsAdapter) {
this.mSavedTripsAdapter = mSavedTripsAdapter;
}
When you create object SavedTripsChanger
pass it the mSavedTripsAdapter instance, either in the constructor or using the setter method. Inside FragmentSavedTrips
SavedTripsChanger mSavedTripsChanger = new SavedTripsChanger();
mSavedTripsChanger.setSavedTripsAdapter(sta);
now you are ready to use it. After you are done modifying the data and ready to update the view inside SavedTripsChanger
just call the notifyDataSetChanged()
mSavedTripsAdapter.notifyDataSetChanged();