簡體   English   中英

ProgressBar - 不在AsyncTask中更新

[英]ProgressBar - Not Updating in AsyncTask

嗨,我剛才有一個關於為什么我的進度條沒有更新的快速問題。 我將在下面添加注釋,以演示哪些有效,哪些無效。

據我所知,它應該工作,因為它在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;
    }
}

publishProgress(Progress ...)調用onProgressUpdate(Progress ...)

在調用publishProgress(Progress ...)之后在UI線程上調用onProgressUpdate(Progress ...)。 執行的時間是不確定的。 此方法用於在后台計算仍在執行時顯示用戶界面中的任何形式的進度。 例如,它可用於為進度條設置動畫或在文本字段中顯示日志。

所以基本上你需要從onProgressUpdate方法更新UI線程。

這是一個例子:

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

這部分是錯的

progress.setProgress((int) songinfo.getProgress());
publishProgress();

您需要從UI線程更新進度條。 因此,要更新進度,必須覆蓋在UI線程上運行的onProgressUpdate ,並從那里更新進度條。

doInBackground ,執行此操作

publishProgress((int) songinfo.getProgress()); // this calls onProgressUpdate on the UI thread

然后,在onProgressUpdate ,執行此操作

progress.setProgress(values[0]);  // called on UI thread

您還需要更改AsyncTask類定義

private class UpdateProgressBar extends AsyncTask<Void, Integer, Void> { // Integer progress type

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM