Android progressBar setVisibility() not working

元气小坏坏 提交于 2019-11-28 02:24:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!