您刚才有一个关于为什么我的进度条没有更新的快速问题。我将在下面添加注释,以演示哪些有效,哪些无效。
据我所知,它应该工作,因为它在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;
}
}
答案 0 :(得分:2)
publishProgress(Progress ...)调用onProgressUpdate(Progress ...)
调用后,在UI线程上调用了onProgressUpdate(Progress ...) publishProgress(进展...)。执行的时间是 未定义。此方法用于显示任何形式的进度 后台计算仍在执行时的用户界面。 例如,它可用于为进度条设置动画或显示登录 文本字段。
所以基本上你需要从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");
}
}
答案 1 :(得分:1)
这部分错了
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