ProgressBar - 不更新AsyncTask

时间:2015-05-04 05:52:50

标签: java android android-asynctask

我最近发布了这个问题并尝试解决这个问题,但我实施了一些不起作用的更改。 (进度条永远旋转圈)

以下代码将被注释以显示问题。没有抛出任何错误,它似乎无法正常工作。 Progressbar已实现并传递给AsyncTask,后者使用publishProgress并将其传递给OnProgressUpdated(),后者尝试更新进度(progressbar)。

谢谢大家。

@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);
        new UpdateProgressBar(holder.bar, tempValue1).execute();
// here is where the asynctask starts passing it the reference to progress bar!

    }

    return view;
}


private class UpdateProgressBar extends AsyncTask<Void, Integer, 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()) {
//this will be set to false when its done no problems here 
            publishProgress((int) songinfo.getProgress());
 // sending the message to onProgressUpdate
            try {
                Thread.sleep(500);
            } catch (Exception e) {

            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        System.out.println("progress: "+values[0]);
// prints the values just fine!
        progress.setProgress(values[0]);
// setting the progressbar here!
    }
}

另外如果我在通过progress.setProgress()设置进度条后添加了system.out.println(progress.getProgress()),我总是得到0

2 个答案:

答案 0 :(得分:0)

请将此方法添加到AsyncTask:

protected void onPostExecute(Long result) 
{
     _yourProgressBar.dismiss();
}

答案 1 :(得分:0)

正在寻找水平进度条