Iban Arriola
Iban Arriola

Reputation: 2776

progressDialog in AsyncTask stop spinning

I am trying to put a ProgressDialog in an AsyncTask with spinning while it loads some info. The problem is that it starts well but suddenly it stops spinning, in one moment it seems that the app is not working any more. The spinning just get stack until when all the info is loaded it disappears and the information is showed properly. Here is the AsyncTask that I am using:

   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(); 
    }

}


  }

Upvotes: 2

Views: 1871

Answers (2)

Janmejoy
Janmejoy

Reputation: 2721

    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");
     }
 }

Check this link for more information

Upvotes: 2

MuraliGanesan
MuraliGanesan

Reputation: 3261

Try this,

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();
        }


    }
}

Upvotes: 0

Related Questions