0

バックグラウンドでsetContentViewによってレイアウトをロードしているときにダイアログを表示するコードを実装しようとしています.、しかし困難を抱えています。

ビューの UI 要素用に定義されたクラス変数と、データベースから別のスレッドにロードされるデータ用の文字列があります。

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

ハンドラーも定義しています。

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);
    ...
    }
};

onCreate では、ダイアログを表示してから、読み込みスレッドを生成しようとしています:

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();
}

これによりデータが読み込まれますが、進行状況ダイアログは表示されません。呼び出しをシャッフルして別のスレッドを生成し、ダイアログを表示しようとしましたが、ダイアログを表示できません。これはかなり一般的なリクエストのようで、この投稿が唯一の回答例のようです。

編集:参考までに、私が最終的にこれを機能させる方法を示すブログ投稿

4

1 に答える 1

1

あなたが望むものはかなり単純なので、AsyncTaskを使用することをお勧めします。onPreExecute() と onPostExecute() でダイアログの表示/非表示を制御できます。リンクをチェックしてください。そこに良い例があります。

于 2011-02-27T14:21:57.587 に答える