2015-05-04 43 views
-1

我最近发布了这个问题,并试图让它解决,但我实施了一些不起作用的更改。 (进度条永远是旋转的圆圈)ProgressBar - 不更新AsyncTask

下面的代码将被注释以显示问题。没有错误抛出,它似乎不工作。 Progressbar实现并传递给使用publishProgress的AsyncTask,并将其传递给尝试更新进度的OnProgressUpdated()(进度条)。

谢谢大家。

@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! 
    } 
} 

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

+1

究竟是不是工作? ProgressBar没有显示?(如果是这样,你忘了调用'progress.show()') – hrskrs

+0

它显示为一个圆圈 –

+0

你需要在任务完成时调用ProgressBar实例上的dismiss()。 – WannaBeGeek

回答

0

一直在寻找一个水平进度

0

请此方法添加到您的AsyncTask:

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

这没有帮助 –