2

我试图在加载一些信息时将 ProgressDialog 放入 AsyncTask 并旋转。问题是它启动良好,但突然停止旋转,一瞬间,该应用程序似乎不再工作了。旋转只是获取堆栈,直到加载所有信息时它消失并且信息正确显示。这是我正在使用的 AsyncTask:

   public class LoadCarsTask extends AsyncTask<String, Car, List<Car>>{

    private SearchFragmentActivity sma;
    private ListFragment fragment;

    public LoadCarsTask (ListFragment fragment){
        this.fragment = fragment;
        dialog = new ProgressDialog(fragment.getActivity());
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        adapter = new SearchCarListAdapter(SearchCarListActivity.this.getActivity(), showedCars, showedDistance, showedReviews, showedPhotos);
        dialog.setMessage("Finding Cars...");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    public SearchFragmentActivity getSma() {
        return sma;
    }

    public void setSma(SearchFragmentActivity sma) {
        this.sma = sma;
    }



    @Override
    protected List<Car> doInBackground(String... params) {
        maxPerHour = Double.parseDouble(filterparams[0]);
        maxPerKm = Double.parseDouble(filterparams[1]);
        maxDistance = Double.parseDouble(filterparams[2]);
        longitude = Double.parseDouble(filterparams[3]);
        latitude = Double.parseDouble(filterparams[4]);
        URL url;
        try {
            //A long calculation goes here
        return cars;
    }

    @Override
    protected void onProgressUpdate(Car... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);         
    }

    @Override
    protected void onPostExecute(List<Car> result) {
        super.onPostExecute(result);
        SearchFragmentActivity.setCars(cars);
        sfa.setUpDistances();
        distance = SearchFragmentActivity.getDistances();
        setUpReviews();
        setUpPhotos();
        while (carsCount<carsNumber && carsCount<cars.size()){
            showedCars.add(cars.get(carsCount));
            showedDistance.add(distance.get(carsCount));
            showedPhotos.add(photos.get(carsCount));
            showedReviews.add(reviews.get(carsCount));
            carsCount++;
        }
        adapter = new SearchCarListAdapter(SearchCarListActivity.this.getActivity(), showedCars, showedDistance, showedReviews, showedPhotos);
        adapter.setCurrentLocation(loc);
        setListAdapter(adapter);
        if(dialog.isShowing())
            dialog.dismiss();
        //sma.showAvailableCarsOnMap(); 
    }

}


  }
4

2 回答 2

2
    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

检查链接以获取更多信息

于 2013-01-28T13:47:00.720 回答
0

尝试这个,

protected class GetTask extends AsyncTask<Void, Void, Integer> {

    protected void onPreExecute() {
    ProgressDialog  mProgressDialog = ProgressDialog.show(MainActivity.this,
                "", "Finding cars");
    }

    @Override
    protected Integer doInBackground(Void... params) {
        // TODO Auto-generated method stub
        //Do your stuff

        return 0;
    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);


        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }


    }
}
于 2013-01-28T13:26:33.690 回答