Wywołanie Android notifyDataSetChanged z AsyncTask
Mam niestandardowy ListAdapter, który pobiera dane z Internetu w AsyncTask.
Dane są dodawane idealnie do listy, ale gdy próbuję wykonać operacje, aplikacja ulega awarii ...
Jestem pewien, że to dlatego, że dzwonię notifyDataSetChanged (); w niewłaściwym czasie (tj. przed zakończeniem AsyncTask).
Co mam teraz:
public class MyListAdapter extends BaseAdapter {
private ArrayList<String> mStrings = new ArrayList<String>();
public MyListAdapter() {
new RetreiveStringsTask().execute(internet_url);
//here I call the notify function ****************
this.notifyDataSetChanged();
}
class RetreiveStringsTask extends AsyncTask<String, Void, ArrayList<String>> {
private Exception exception;
@Override
protected ArrayList<String> doInBackground(String... urls) {
try {
URL url= new URL(urls[0]);
//return arraylist
return getStringsFromInternet(url);;
} catch (Exception e) {
this.exception = e;
Log.e("AsyncTask", exception.toString());
return null;
}
}
@Override
protected void onPostExecute(ArrayList<String> stringsArray) {
//add the tours from internet to the array
if(stringsArray != null) {
mStrings.addAll(toursArray);
}
}
}
}
Moje pytanie brzmi: czy mogę wywołać notifyDataSetChanged () z funkcji onPostExecute w AsyncTask lub w dowolnym innym czasie, gdy AsyncTask pobiera dane?