2

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

}


  }
Iban Arriola
  • 2,526
  • 9
  • 41
  • 88

2 Answers2

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

Check this link for more information

Janmejoy
  • 2,721
  • 1
  • 20
  • 38
  • I try this and it just do nothing. But I don´t understand if I want the spinning from the start why you put it in onPostExecute. Anyway it is not even appearing so I think that is not what I need. – Iban Arriola Jan 28 '13 at 16:16
  • http://stackoverflow.com/questions/6450275/android-how-to-work-with-asynctasks-progressdialog try this link – Janmejoy Jan 29 '13 at 05:32
  • Thanks for trying to help me but it is not working... actually I get an error trying to use publicProgress and I cannot even compile the app. – Iban Arriola Jan 29 '13 at 09:58
  • I have been debugging it and I found that is spinning well until it arrives to onPostExecute and there it stacks until finally it disappears when all the information is load. – Iban Arriola Jan 29 '13 at 10:08
0

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


    }
}
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
  • It makes me the same... it start spinning but suddenly it stops and it is like blocked until it finally disappear and a all the data appears. – Iban Arriola Jan 28 '13 at 16:18