Reputation: 2142
I have a listview of items (ItemSong) , which when clicked start downloading (using Asynctask). I have a textview that shows the download progress.
ItemSong has an int property (percentCompleted) that tracks the download progress. DownloadTask updates this int. So this way, when I scroll back to the item, the progress is shown. But it doesn't continuously update even though the DownloadTask is running and the percentCompleted is being updated.
Everything works fine, but when I scroll down and back i.e. item is recycled, the progress stops updating. It shows the last updated valueonly when I scroll away and back to the item.
I have trimmed the code to the necessary parts, but if something is unclear I add more or explain it.
Any suggestions on what I could, even if I need to change the whole approach, is appreciated. Thanks.
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;}
}
My getView() in the Adapter class: I have a set a flag that updates the textview is the download is active. However this causes the app to freeze.
TextView percentCompleted = (TextView)v.findViewById(R.id.completed);
while (itemSong.downloadStatus() == true)
{
percentCompleted.setText(Integer.toString(itemSong.getCompleted() ) );}
onItemClick in my MainActivity
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();
}
The DownloadTask class:
class DownloadTask extends AsyncTask<String, String, String> {
private TextView percentComplete;
private int prog=0;
private ItemSong itemSong;
DownloadTask(TextView percentComplete, ItemSong itemSong){
this.percentComplete = percentComplete;
this.itemSong = itemSong;
}
protected String doInBackground(String... f_url) {
itemSong.setDownloadStatus(true);
while (prog <= 100) {
itemSong.setCompleted(prog);
publishProgress("" + (prog));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
prog = prog+10;
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
// setting progress percentage
percentComplete.setText(progress[0].toString());
}
@Override
protected void onPostExecute(String file_url) {
itemSong.setDownloadStatus(false);
}
}
Upvotes: 0
Views: 85
Reputation: 1366
as i said in comments above after every recycle , progress value of downloading fields that are visible in ListView must be updated .by using some type of array like hashMap ,progress values and positions of downloading item must be cached in adapter class then at recycle time use them and update list Items . in this particular case after doing it , must call notifyDataSetChanged()
in main Thread of program and in asynctask in onProgressUpdate :
@Override
protected void onProgressUpdate(String... progress) {
// setting progress percentage
percentComplete.setText(progress[0].toString());
yourListView.notifyDataSetChanged();//
}
Upvotes: 1