I know how to create a list with View
like the ones shown above. I am attempting to create a downloader and there will be multiple threads downloading at the same time.
These Thread
s needs to use the setProgress(..)
of the ProgressBar
. However, I do not know how I can refer to these progress bars once they have been created using an adapter. And, by extension, the cancel buttons so that I can add listeners to it.
Please help me with this
I would suggest you to create Task
class, which would implement Runnable
and have a method getCurrentProgress()
, which would return progress of current task and a method cancel()
to cancel itself (canceling can be done in any desired way, by raising a flag or any other way). Then you can start a task using ordinary thread (or use AsyncTask, or one of Executors
) and add your Task
s to adapter as an item, and then you can display appropriate Views by using
Task task = getItem(position);
progressBar.setProgress(task.getCurrentProgress());
in your getView()
method of adapter.
Then when you need to update progress you just need to update progress inside Task
and call adapter.notifyDataSetChange()
on the UI thread. What you also need to do is to ensure that your new progress in Task
instance would be visible from other threads, for this I would suggest you using AtomicInteger
as progress indicator (because every AtomicInteger.get()
returns the latest value).