AsyncTask ProgressDialog不会更新对话框消息

时间:2011-10-08 11:29:03

标签: android multithreading user-interface

我的应用中存在长ProgressDialog的问题。我想在第一次启动应用程序时安装一个相当大的数据库(转换成文本文档,数据库将不到200页)。显然,这需要一些时间,因此我希望在此过程中向用户显示ProgressDialog

我正试图用AsyncTask完成此任务;我之前曾尝试使用Thread,但我遇到了与保存/恢复状态相关的问题。

进度条更新就好了。但是,我正在分三个阶段安装数据库,我想分别展示这些阶段,因此用户对正在发生的事情有一些了解,因此条形图上的每个百分点都不需要这么长时间。由于某种原因,虽然我能够在对话框中设置进度,但设置消息(或标题)似乎没有做任何事情。

这是我的AsyncTask子类的代码:

public class InstallDbTask extends AsyncTask<Void, Integer, Void>
{
    private static final String TAG = "InstallDbTask";
    private static final int READ_RULES = 0;
    private static final int WRITE_RULES = 1;
    private static final int RW_GLOSSARY = 2;

    private ProgressDialog dialog;
    private Context context;

    public InstallDbTask(Context context) { this.context = context; }

    protected void onPreExecute()
    {
        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(STYLE_HORIZONTAL);
        dialog.setCancelable(false);
        dialog.show();
    }

    protected Void doInBackground(Void... params)
    {
        ... Initialization ...

        // Number of actions to perform for progress bar
        int totalCount = rules.length;
        // Literal increment for each action
        int incrementCount = 0;
        // Integral percentage: incrementCount / totalCount * 100
        int total = 0;

        // Read rules from XML
        for(int i = 0; i < rules.length; i++)
        {
            ... Parse XML ...

            incrementCount++;
            total = (int)(((float)incrementCount / totalCount) * 100);
            publishProgress(total, READ_RULES);
        }

        incrementCount = 0;

        // Write rules to DB
        SQLiteDatabase database = new DbOpenHelper(context).getWritableDatabase();
        for(Rule r : rulesList)
        {
            ... Insert ...

            incrementCount++;
            total = (int)(((float)incrementCount / totalCount) * 100);
            publishProgress(total, WRITE_RULES);
        }

        totalCount = glossaryTerms.length;
        incrementCount = 0;

        // Write glossary to DB
        String term = null;
        for(int i = 0; i < glossaryTerms.length; i++)
        {
            ... Insert ...

            incrementCount++;
            total = (int)(((float)incrementCount / totalCount) * 100);
            publishProgress(total, RW_GLOSSARY);
        }
        database.close();

        return null;
    }

    protected void onProgressUpdate(Integer... progress)
    {
        dialog.setProgress(progress[0]);
        switch (progress[1])
        {
            case READ_RULES:
                dialog.setMessage(context.getString(R.string.load_db1));
                break;
            case WRITE_RULES:
                dialog.setMessage(context.getString(R.string.load_db2));
                break;
            case RW_GLOSSARY:
                dialog.setMessage(context.getString(R.string.load_db3));
                break;
        }
    }

    protected void onPostExecute(Void result)
    {
        dialog.dismiss();
    }
}

onProgressUpdate方法中的案例正确到达,ProgressDialog只是没有收到消息,可以这么说。有什么我做错了吗?

1 个答案:

答案 0 :(得分:3)

您必须在显示对话框之前设置初始消息。否则在dialog.setMessage()中调用onProgressUpdate()无效。

要修复代码,您必须在调用dialog.setMessage("Some initial message")之前添加""(消息字符串可以是dialog.show(),但不能为空):

protected void onPreExecute()
{
    dialog = new ProgressDialog(context);
    dialog.setProgressStyle(STYLE_HORIZONTAL);
    dialog.setCancelable(false);
    dialog.setMessage("Installing database, please wait");
    dialog.show();
}