0

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() を呼び出すことができますか?

4

3 に答える 3

5

AsyncTask の onPostExecute 関数から notifyDataSetChanged() を呼び出すことはできますか

はい、実行が完了したらnotifyDataSetChanged()、からonPostExecuteUpdate Adapter データを呼び出すことができます。doInBackground次のようにします。

@Override
protected void onPostExecute(ArrayList<String> stringsArray) {
    //add the tours from internet to the array
    if(stringsArray != null) {
        mStrings.addAll(toursArray);
        // call notifyDataSetChanged() here...
         MyListAdapter.this.notifyDataSetChanged();
    }
}
于 2013-05-08T12:58:15.287 に答える
2

として電話notifyDataSetChanged()をかけるonPostExecute()

@Override
        protected void onPostExecute(ArrayList<String> stringsArray) {
            //add the tours from internet to the array
            if(stringsArray != null) {
                mStrings.addAll(toursArray);
MyListAdapter.this.notifyDataSetChanged();
            }
        }
于 2013-05-08T12:58:03.503 に答える
1

onPostExecuteのメソッドで呼び出してみましたかASyncTaskonPreExecuteおよび onは、onPostExecuteUI を更新するために使用されます。

于 2013-05-08T12:59:06.117 に答える