Android progressBar setVisibility() not working

后端 未结 1 1865
小鲜肉
小鲜肉 2020-12-07 06:24

Can someone please help me understand why progressBar.setVisibility() works when using a CountDownTimer but not when doing async downl

相关标签:
1条回答
  • 2020-12-07 06:29

    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.

    0 讨论(0)
提交回复
热议问题