考え方は単純です。新しい TextView を作成し、それを ListView にアタッチしてから、サーバーからコンテンツのダウンロードを開始します。コンテンツのダウンロードがない場合、ListView は即座に更新されます。しかし、ダウンロードを開始した ListView に TextView を追加した後、ダウンロードが終了すると、画面上の listView が更新されます。
主な機能
TextView startingDownloadingText = createTextView("Downloading started");
linearLayout.addView(startingDownloadingText);
boolean success = downloadFromUrl(stringUri, fileName, context);
if (success) {......
ダウンロード機能
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;
}
}
なぜこうなった ?
アップデート
AsyncTask<...>.get() は UI ブロッキングであり、AsyncTask で onPostExecute() を使用すると、非ブロッキングの方法で作業が行われます。例: UI にコールバックを送信する android asynctask