我有一个项目列表视图(ItemSong),点击后开始下载(使用Asynctask)。我有一个textview显示下载进度。
ItemSong有一个跟踪下载进度的int属性(percentCompleted)。 DownloadTask更新此int。所以这样,当我滚动回项目时,会显示进度。但即使正在运行DownloadTask并且正在更新percentCompleted,它也不会持续更新。
一切正常,但当我向下滚动并返回项目被回收时,进度停止更新。当我滚动并返回项目时,它仅显示上次更新的值。
我已将代码修剪为必要的部分,但如果有些不清楚,我会添加更多或解释它。
对于我能做什么的任何建议,即使我需要改变整个方法,也表示赞赏。感谢。
refreshCourtLine.php
Adapter类中的 getView(): 我有一个标志,更新textview是下载是活动的。但是这会导致应用冻结。
public class ItemSong implements Item{
public final String track_name;
public final String album_name;
public final String track_num;
public final String album_id;
public final String album_link;
private int percentCompleted = 0;
private boolean activeDownload; //false by default
public ItemSong(String track_name, String album_name, String track_num, String album_id, String album_link) {
this.track_name = track_name;
this.album_name = album_name;
this.track_num = track_num;
this.album_id = album_id;
this.album_link = album_link;
}
@Override
public boolean isSection() {
return false;
}
public String itemType() {
return "song";
}
public void setCompleted(int percent){ percentCompleted = percent; }
public int getCompleted() {return percentCompleted;}
public void setDownloadStatus(boolean status) {activeDownload = status;}
public boolean downloadStatus() {return activeDownload;}
}
我的MainActivity中的onItemClick
TextView percentCompleted = (TextView)v.findViewById(R.id.completed);
while (itemSong.downloadStatus() == true)
{
percentCompleted.setText(Integer.toString(itemSong.getCompleted() ) );}
DownloadTask类:
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
ItemSong itemSong = (ItemSong) items.get(position);
TextView percentCompleted = (TextView) arg1.findViewById(R.id.completed);
new DownloadTask(percentCompleted, itemSong).execute();
}
答案 0 :(得分:1)
notifyDataSetChanged()
并在onProgressUpdate中调用asynctask:
@Override
protected void onProgressUpdate(String... progress) {
// setting progress percentage
percentComplete.setText(progress[0].toString());
yourListView.notifyDataSetChanged();//
}