From 8a8dc0a2ac4f3552568d9e14bf5ae121394b055c Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 17 Aug 2010 02:58:24 +0200 Subject: fix progressdialog loading of the list in own thread refresh ui afterwards with handler (only first thread can change the view) --- .../animux/android/andmal/AnimeListActivity.java | 57 +++++++++++++++------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/de/animux/android/andmal/AnimeListActivity.java b/src/de/animux/android/andmal/AnimeListActivity.java index 8db6635..566fa1d 100644 --- a/src/de/animux/android/andmal/AnimeListActivity.java +++ b/src/de/animux/android/andmal/AnimeListActivity.java @@ -3,6 +3,7 @@ package de.animux.android.andmal; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; +import java.util.List; import java.util.Map; import de.animux.android.andmal.api.anime.Anime; @@ -14,6 +15,8 @@ import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.widget.ListView; import android.widget.SimpleAdapter; @@ -24,6 +27,10 @@ public class AnimeListActivity extends Activity { public final static int INITIAL_SYNC = 0; + private AnimeList animeList; + + private UpdateUIHandler updateUIHandler = new UpdateUIHandler(); + public Map createItem(String title, String caption) { Map item = new HashMap(); item.put(ITEM_TITLE, title); @@ -31,6 +38,26 @@ public class AnimeListActivity extends Activity { return item; } + private class UpdateUIHandler extends Handler { + @Override + public void handleMessage(Message msg) { + SeparatedListAdapter adapter = new SeparatedListAdapter( + AnimeListActivity.this); + Map> animes = animeList + .getAnimes(); + + for (State s : State.values()) + addAnimes(adapter, animes.get(s), s.toString()); + + ListView list = new ListView(AnimeListActivity.this); + list.setAdapter(adapter); + + dismissDialog(INITIAL_SYNC); + + AnimeListActivity.this.setContentView(list); + } + } + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { @@ -38,25 +65,18 @@ public class AnimeListActivity extends Activity { showDialog(INITIAL_SYNC); - AnimeList animeList = null; - try { - animeList = new AnimeList("AlexanderS"); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - dismissDialog(INITIAL_SYNC); + new Thread() { + public void run() { + try { + animeList = new AnimeList("AlexanderS"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - SeparatedListAdapter adapter = new SeparatedListAdapter(this); - Map> animes = animeList.getAnimes(); - - for (State s : State.values()) - addAnimes(adapter, animes.get(s), s.toString()); - - ListView list = new ListView(this); - list.setAdapter(adapter); - this.setContentView(list); + updateUIHandler.sendEmptyMessage(0); + } + }.start(); } private void addAnimes(SeparatedListAdapter adapter, @@ -82,6 +102,7 @@ public class AnimeListActivity extends Activity { case INITIAL_SYNC: ProgressDialog dialog = ProgressDialog.show(this, "", "Loading list. Please wait...", true); + dialog.show(); return dialog; default: return super.onCreateDialog(id); -- cgit v1.2.3