Can someone please help me understand why progressBar.setVisibility()
works when using a CountDownTimer
but not when doing async
downl
The answer is to put the whole for
loop in the onClick actionJson()
into a worker thread as follows as noted here:
public void actionJson(View view) {
Toast.makeText(this, "starting progressBar - json fetch", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.VISIBLE);
final String urlTemplate = "https://hacker-news.firebaseio.com/v0/item/___ITEM_NUMBER___.json?print=pretty";
counter = 0;
// simply put it in a worker thread does the job
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < items.length; i++) {
String url = urlTemplate.replaceAll("___ITEM_NUMBER___", String.valueOf(items[i]));
//Log.i("actionJson", url);
DownloadJson downloadJson = new DownloadJson();
try {
downloadJson.execute(url).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}).start();
}
So even though the for
loop as originally defined does spawn a whole bunch of asyncTasks
individually, it is still somehow blocking the main thread.