ProgressBar – Not Updating in AsyncTask

Hi just had a quick question about why my progressbar isn’t updating. I will add comments in the below to demonstrate what is working and what isn’t.

To my knowledge it should be working since it updates in an asynctask.

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (!(data.get(position) instanceof TemporarySongInfomation)) {
        SongViewHolder holder;
        view = inflater.inflate(R.layout.music_list_format, null);
        holder = new SongViewHolder();
        holder.timesplayed = (TextView) view.findViewById(R.id.textView7);
        holder.artist = (TextView) view.findViewById(R.id.textView6);
        holder.title = (TextView) view.findViewById(R.id.textView5);
        holder.imagebutton = (ImageButton) view.findViewById(R.id.playbutton);
        holder.source = (TextView) view.findViewById(R.id.textView8);
        tempValue = (SongInfomation) data.get(position);
        String songName = tempValue.getName();
        holder.imagebutton.setBackgroundResource(R.drawable.playbutton1);
        holder.source.setText(tempValue.getVideoid());
        holder.title.setText(songName.length() > 45 ? songName.substring(0, 38) + "..." : songName);
        holder.timesplayed.setText("" + tempValue.getTimesplayed());
        holder.artist.setText(tempValue.getArtist());
        swipeDetector = new SwipeDetector();
        view.setOnClickListener(new SongListOnItemClickListener(position));
        view.setOnTouchListener(swipeDetector);
        holder.imagebutton.setOnClickListener(new OnPlayButtonClickListener(position));
    } else {
        TemporarySongViewHolder holder;
        view = inflater.inflate(R.layout.music_list_process_format, null);
        holder = new TemporarySongViewHolder();
        holder.artist = (TextView) view.findViewById(R.id.artisttemp);
        holder.bar = (ProgressBar) view.findViewById(R.id.ppbar);
        holder.title = (TextView) view.findViewById(R.id.titletemp);
        holder.source = (TextView) view.findViewById(R.id.sourcetemp);
        tempValue1 = (TemporarySongInfomation) data.get(position);
        String songName = tempValue1.getName();
        holder.source.setText(tempValue1.getVideoid());
        holder.title.setText(songName.length() > 45 ? songName.substring(0, 38) + "..." : songName);
        holder.artist.setText(tempValue1.getArtist());
        holder.bar.setMax(100);
// the below line starts the task!
        new UpdateProgressBar(holder.bar, tempValue1).execute();

    }

    return view;
}

private class UpdateProgressBar extends AsyncTask<Void, Void, Void> {
    private TemporarySongInfomation songinfo;
    private ProgressBar progress;

    UpdateProgressBar(ProgressBar bar, TemporarySongInfomation tp) {
        progress = bar;
        songinfo = tp;
    }

    @Override
    protected Void doInBackground(Void... params) {
        while (!songinfo.isCompleted()) {
            System.out.println("going " + (int) songinfo.getProgress());
// the above line prints different values for songinfo.getProgress()
            progress.setProgress((int) songinfo.getProgress());
            publishProgress();
            System.out.println("Progress "+progress.getProgress());
// the above line only prints "Progress 0"
// and obviously the ui doesnt update.
            try {
                Thread.sleep(500);
            } catch (Exception e) {

            }
        }
        return null;
    }
}

Answer

publishProgress(Progress…) calls onProgressUpdate(Progress…)

onProgressUpdate(Progress…) invoked on the UI thread after a call to publishProgress(Progress…). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

so basically you need to update the UI thread from onProgressUpdate method.

Here an example:

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