0

The idea is simple. Create a new TextView, attach it to ListView and then start downloading contents from a server. When there is no content downloading, the ListView is instantly updated. But after adding the TextView to the ListView I started downloading, refreshing listView on screen is done after the downloading is finished.

Main function

TextView startingDownloadingText = createTextView("Downloading started");
linearLayout.addView(startingDownloadingText);
boolean success = downloadFromUrl(stringUri, fileName, context);
if (success) {......

Function for downloading

public boolean downloadFromUrl(String stringURL, String fileName, Context myContext) { 

    try {
        URL url = new URL(stringURL);             
        Resources res = myContext.getResources();
        String envDirectory = Environment.getExternalStorageDirectory().toString();
        String zipDirectory = envDirectory.concat(res.getString(R.string.zipDirectory));

        File fileDirectory = new File(zipDirectory);
        fileDirectory.mkdirs();

        ZipDownloader zipDownloader = new ZipDownloader(zipDirectory, fileName);
        AsyncTask<URL, Void, Boolean> asynZipDownloader = zipDownloader.execute(url);
        Boolean success = null;
        try {
            success = asynZipDownloader.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        if (success){
            Log.d("DownloadFromUrl", "file ready");
            return true;
        }
        return false;
    } catch (IOException e) {
        Log.e("DownloadFromUrl", "Error: " + e);
        return false;
    }

}

Why is this happening ?

UPDATE

AsyncTask<...>.get() is UI blocking and using onPostExecute() in AsyncTask will do the work in non blocking way. Example : android asynctask sending callbacks to ui

Community
  • 1
  • 1
Luka Blažecki
  • 183
  • 1
  • 7

1 Answers1

1

You appear to be downloading from the UI Thread, take a look at AsyncTask and try and implement that instead. See here

When you request a download it blocks on the UI Thread until completed. If you put it into a separate AsyncTask then this frees up your UI Thread and you can call back into the UI Thread to provide the result and update your UI

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
  • But in my main function I first call create view and then async download. Shouldn't async download be called after completion of creating the view? – Luka Blažecki Jan 29 '13 at 08:37
  • This is irrelevant, performing network operations on the UI Thread will slow it down whether one comes first or not. In your case it means that the ListView has been created, but you've blocked the Thread from getting to the end of it's draw cycle where it will call onDraw() to actually render the list on the screen. You should do all the UI stuff, then start an Async Task and wait for its return in a callback – biddulph.r Jan 29 '13 at 10:08
  • Don't forget that you CANNOT have network process in the UI thread with StrictMode on 2.3.3+ version of Android. Always use AsyncTask or Thread for that purpose – CinetiK Jan 29 '13 at 12:37