2011-02-27 13 views
0

アクティビティの読み込み中に読み込みダイアログに表示するのが難しいが、showing dialog while loading layout by setContentView in backgroundhttp://developer.android.com/guide/appendix/faq/commontasks.html#threadingのコードを実装しようとしています。私はまた、ハンドラが定義されているAndroid:アクティビティ用の読み込み中のダイアログ

private TextView mLblName, mLblDescription, etc... 
private String mData_RecipeName, mData_Description... 

私は、データベースから別のスレッドにロードされているデータのクラス私の見解ではUI要素のために定義された変数、および、文字列を持っていますonCreateで

private ProgressDialog dialog; 
final Handler mHandler = new Handler(); 
final Runnable mShowRecipe = new Runnable() { 
    public void run() { 
     //setContentView(R.layout.recipe_view); 
    setTitle(mData_RecipeName); 
    mLblName.setText(mData_RecipeName); 
    mLblDescription.setText(mData_Description); 
    ... 
    } 
}; 

、私もその後、積載スレッドを生成、ダイアログを表示しようとしている:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true); 
    setContentView(R.layout.recipe_view); 
    showData(); 
} 

protected void showData() { 
    // Fire off a thread to do some work that we shouldn't do directly in the UI thread 
    Thread t = new Thread() { 
     public void run() { 
      mDatabaseAdapter = new ChickenPingDatabase(ShowRecipe.this); 
     mDatabaseAdapter.open(); 

     mTabHost = getTabHost(); 

     mLblName = (TextView)findViewById(R.id.lblName); 
     mLblDescription = (TextView)findViewById(R.id.lblDescription); 
     ... 

     Cursor c = mDatabaseAdapter.getRecipeById(mRecipeId); 
     if(c != null){ 
      mData_RecipeName= c.getString(c.getColumnIndex(Recipes.NAME)); 
        mData_Description= c.getString(c.getColumnIndex(Recipes.DESCRIPTION)); 
      ... 
        c.close(); 
     } 

     String[] categories = mDatabaseAdapter.getRecipeCategories(mRecipeId); 
     mData_CategoriesDesc = Utils.implode(categories, ","); 

     mHandler.post(mShowRecipe); 

     } 
    }; 
    t.start(); 
} 

これはデータをロードしますが、進捗ダイアログは表示されません。私は別のスレッドを生成し、ダイアログを表示するために呼び出しをシャッフルしようとしましたが、表示するダイアログを取得できません。これはかなり一般的な要求であると思われ、この投稿は答えた唯一の例と思われました。

編集:参考:a blog post demonstrating the way I eventually got this working

+1

問題ありません!自分のブログ記事を投稿したリンクはありますか? onPreExecute()をオーバーライドし、onCreateではなくダイアログを表示することで、AsyncTaskのメソッドをさらに利用できます。オリエンテーションを変更したときにアプリがどのように反応するかをテストしてください。コンテキストが失われないようにAsyncTaskを適切に処理するようにしてください。 @ CommonSoftはここでそれを行う方法についての良いデモを行います:http://bit.ly/gFYzsP – Zarah

答えて

1

何をしたいことは、かなりストレートフォワードなので、私はあなたがAsyncTaskを使用することをお勧めします。 onPreExecute()とonPostExecute()でダイアログの表示/非表示を制御できます。リンクを確認してください。良い例があります。

+0

Wahay。私はこの上に私の髪を引っ張ってきた。ありがとうZarah! – Echilon

関連する問題