Maarten
Maarten

Reputation: 7338

Keeping track of AsyncTask status to update ListView items

I have a ListView that shows a bunch of downloadable objects. When the user clicks a list item, the item gets downloaded through an AsyncTask, and a notification is also made showing the download progress. I also want the progress to show inside the ListView item.

I want behave more or less like Google Drive when you make an item available offline.

My problem: considering situations where the the app is closed before the download is complete, I can't think of a robust way of 'storing' the download status that can be retrieved in getView().

Should onProgressUpdate() best store the download progress in the database, a variable inside the activity or modify the ListView child item directly?

EDIT

For anyone interested, I used a service that sends out a broadcast when the download status changes:

Activity:

private BroadcastReceiver mDataSetChangedReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(downloadDetailFragment!=null){
            downloadDetailFragment.notifyDataSetChanged();
        }
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  LocalBroadcastManager.getInstance(this).registerReceiver(mDataSetChangedReceiver,
      new IntentFilter(DownloadService.DOWNLOAD_STATUS_CHANGED));
}

@Override
protected void onDestroy() {
  super.onDestroy();

LocalBroadcastManager.getInstance(this).unregisterReceiver(mDataSetChangedReceiver); }

AsyncTask:

        Intent intent = new Intent(DownloadService.DOWNLOAD_STATUS_CHANGED);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

Upvotes: 0

Views: 289

Answers (1)

Wenger
Wenger

Reputation: 989

I would try using a Service, which it looks you might be doing anyway if you have a notification tracking the download. If you can track the progress in your Service and send that info to the ListView also, you could show the ProgressBar in both easily.

Using a Service would also let you store the downloaded information using SharedPreferences so you could access it later if the app is closed. Kinda conceptual but I hope this helps.

Upvotes: 1

Related Questions