Java ProgressBar-不更新异步任务
我最近发布了这个问题,并试图解决它,但我实施了一些改变,但没有奏效。(进度条永远在旋转) 下面的代码将被注释以显示问题。没有抛出错误,只是看起来不起作用。Progressbar已实现并传递给使用publishProgress的AsyncTask,并将其传递给尝试更新进度的OnProgressUpdate()(Progressbar) 谢谢大家Java ProgressBar-不更新异步任务,java,android,android-asynctask,Java,Android,Android Asynctask,我最近发布了这个问题,并试图解决它,但我实施了一些改变,但没有奏效。(进度条永远在旋转) 下面的代码将被注释以显示问题。没有抛出错误,只是看起来不起作用。Progressbar已实现并传递给使用publishProgress的AsyncTask,并将其传递给尝试更新进度的OnProgressUpdate()(Progressbar) 谢谢大家 @Override public View getView(final int position, View convertView, ViewGroup
@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!
}
}
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图=转换视图;
if(!(数据获取(位置)临时歌曲信息实例)){
视窗支架;
视图=充气机。充气(R.layout.music\u list\u格式,空);
holder=新歌曲视图holder();
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=(歌曲信息)data.get(位置);
字符串songName=tempValue.getName();
支架.图像按钮.立根资源(R.可绘制.播放按钮1);
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=新swipeDetector();
view.setOnClickListener(新歌曲列表OnItemClickListener(位置));
view.setOnTouchListener(swipeDetector);
holder.imagebutton.setOnClickListener(新的onPlayButtonClicklListener(位置));
}否则{
临时视窗座;
视图=充气机。充气(R.layout.music\u list\u process\u format,null);
holder=新的临时SongViewHolder();
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=(TemporarySongInformation)data.get(位置);
字符串songName=tempValue1.getName();
holder.source.setText(tempValue1.getVideoid());
holder.title.setText(songName.length()>45?songName.substring(0,38)+“…”:songName);
holder.artist.setText(tempValue1.getArtist());
固定杆最大设定值(100);
新的UpdateProgressBar(holder.bar,tempValue1.execute();
//这里是asynctask开始向其传递对进度条的引用的地方!
}
返回视图;
}
私有类UpdateProgressBar扩展了AsyncTask{
私人临时歌曲信息;
私人进度条进度;
UpdateProgressBar(进度条,临时歌曲信息tp){
进度=巴;
songinfo=tp;
}
@凌驾
受保护的Void doInBackground(Void…参数){
而(!songinfo.isCompleted()){
//当它在这里没有问题时,它将被设置为false
publishProgress((int)songinfo.getProgress());
//将消息发送到onProgressUpdate
试一试{
睡眠(500);
}捕获(例外e){
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(整型…值){
System.out.println(“进度:+值[0]);
//打印值很好!
progress.setProgress(值[0]);
//在这里设置进度条!
}
}
另外,如果我在通过progress.setProgress()设置progressbar后添加system.out.println(progress.getProgress()),我总是得到0,请将此方法添加到异步任务中:
protected void onPostExecute(Long result)
{
_yourProgressBar.dismiss();
}
正在寻找水平进度条到底什么不起作用?ProgressBar未显示?(如果是这样,您忘记调用
progress.show()
)它显示为一个圆圈。任务完成时,您需要在ProgressBar实例上调用dismise()。dismission并不是它不更新System.out.println中的值的原因(“进度:+值[0]);