Android:加载活动对话框

发布于 2024-10-19 23:36:51 字数 2504 浏览 2 评论 0原文

我正在尝试实现 ​​显示对话框中的代码通过 setContentView 在后台加载布局http://developer .android.com/guide/appendix/faq/commontasks.html#threading 在我的活动正在加载但遇到困难时显示加载对话框。

我为视图中的 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();
}

这会加载数据,但不显示进度对话框。我尝试过重新排列调用以生成单独的线程并显示对话框,但无法显示对话框。看来这是一个相当常见的请求,这篇文章似乎是唯一得到答复的示例。

编辑:供参考,一篇博客文章演示了我最终如何实现此功能

I'm trying to implement the code from showing dialog while loading layout by setContentView in background and http://developer.android.com/guide/appendix/faq/commontasks.html#threading to show a loading dialog while my activity is loading, but having difficulty.

I have class variables defined for the UI elements in my view, and also strings for the data which is loaded on another thread from the database:

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

I also have the handlers defined:

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

In onCreate, I'm trying too show the dialog, then spawn the loading thread:

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

This loads the data, but the progress dialog isn't shown. I've tried shuffling the call to spawn the separate thread and show the dialog around, but can't get the dialog to show. It seems this is a fairly common request, and this post seemed to be the only answered example of it.

EDIT: For reference, a blog post demonstrating the way I eventually got this working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-10-26 23:36:51

由于您想要的非常简单,因此我建议您使用 AsyncTask 。您可以在 onPreExecute() 和 onPostExecute() 中控制对话框的显示/隐藏。查看链接,里面有一个很好的例子。

Since what you want is pretty much straight-forward, I would recommend that you use an AsyncTask. You can control the showing/hiding of the dialog in onPreExecute() and onPostExecute(). Check out the link, there's a good example in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文