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