AsyncTask でインターネットからデータをフェッチするカスタム ListAdapter があります。
データはリストに完全に追加されますが、操作をしようとするとアプリケーションがクラッシュします...
これは、notifyDataSetChanged(); を呼び出しているためだと確信しています。間違った時間に (つまり、AsyncTask が終了する前に)。
私が今持っているもの:
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);
}
}
}
}
私の質問は、AsyncTask の onPostExecute 関数から、または AsyncTask がデータを取得したときにいつでも notifyDataSetChanged() を呼び出すことができますか?