android - ProgressDialog 没有显示在我的 ListActivity 的 onListItemClick 方法中

标签 android listactivity progressdialog

我有 ListActivityBaseAdapter。每次用户单击一个项目时,我都会清除适配器并重新加载新内容(这是某种树逻辑)。

有时在 onListItemClick() 中适配器获取新的项目列表需要几秒钟。我想在输入 onListItemClick() 时显示一些进度条,并在完成时隐藏它。

为什么我的示例中没有显示进度对话框?我怀疑(正如我所读)问题是因为“计算”没有在后台线程中运行?如果是这样,有人可以根据我的代码给我一个例子吗?

代码如下:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    catalogAdapter.previousUrl = catalogAdapter.currentUrl;
    RRmlMappingServicesCatalogItem item = catalogAdapter.getItem(position);
    catalogAdapter.currentUrl = item.href;
    //catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()), item.href, null, 120000, 500000);
    loadCatalogFromRml(this.getApplicationContext(), item.href);
    catalogAdapter.clearData();

    if ((catalogAdapter.mappingServicesCatalog.getFlatItems()).length == 0)
    {
        catalogAdapter.mappingServiceContent = new RRmlMappingServiceContent ();
        this.loadContentFromRml(this.getApplicationContext(), item.href);
        //catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(this.getApplicationContext()),item.href, null, 120000, 500000);
        settings = (GlobalSettings) getApplication();
        settings.setMappingServiceContent(catalogAdapter.mappingServiceContent);
        if (catalogAdapter.mappingServiceContent.authentication != null)
        {
            startActivity(new Intent(CatalogActivity.this,
                LoginActivity.class));
        }
        else
        {
            startActivity(new Intent(CatalogActivity.this,
                    MapActivity.class));
        }
    }
    else
    {
        for (RRmlMappingServicesCatalogItem s : catalogAdapter.mappingServicesCatalog.getFlatItems()) {
            catalogAdapter.addItem(s);
        }
        setListAdapter(catalogAdapter);
    }
}

public void loadContentFromRml(Context context, final String url)
{
    final Context myContext = context;
    final String myUrl = url;

        progressDialog = ProgressDialog.show(CatalogActivity.this, "",
            "Loading content...", true);

    new Runnable()
    {       
        public void run() 
        {
            catalogAdapter.mappingServiceContent.loadFromRml(new RIoConnectionManagerImpl(myContext),myUrl, null, 120000, 500000);
            progressDialog.dismiss();
        }
    }.run();
}

public void loadCatalogFromRml(Context context, String url)
{
    final Context myContext = context;
    final String myUrl = url;

        progressDialog = ProgressDialog.show(CatalogActivity.this, "",
            "Loading catalog...", true);

    new Runnable()
    {       
        public void run() 
        {
            catalogAdapter.mappingServicesCatalog.loadFromRml(new RIoConnectionManagerImpl(myContext), myUrl, null, 120000, 500000);
            progressDialog.dismiss();
        }
    }.run();
}

最佳答案

我怀疑您的代码中最繁忙的一行是:

catalogAdapter.mappingServicesCatalog.loadFromRml(...);

为了显示对话框,您需要从调用 ProgressDialog.show(...) 的函数返回。那是因为这个调用的实际处理是在消息循环中完成的(这个循环首先调用了你的函数)。因此,在您的代码中,您正在创建一个对话框并将其标记为要显示,但从来没有真正通过在同一函数的末尾调用 .dismiss() 来让它有机会显示自己。

您应该使用 AsyncTask。首先,您需要将 UI 处理代码与 loadFromRml 功能分开。将所有耗时的东西放在AsyncTaskdoInBackground函数中,所有UI应该在onPreExecuteonPostExecute中。

您可以在 onListItemClick 函数中显示进度对话框,并在 AsyncTask.onPostExecute 函数中关闭它。或者,作为替代方案,在 AsyncTask.onPreExecute 中显示进度对话框并将其隐藏在 AsyncTask.onPostExecute 中。

AsyncTask 有很多例子,其中一个位于AsycnTask android docs .可以在 android 开发人员资源中找到另一篇有用的文章:Painless Threading .

关于android - ProgressDialog 没有显示在我的 ListActivity 的 onListItemClick 方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6354016/

相关文章:

android - 在 Activity 中使用 SharedPreferences 和/或类变量

android - java.lang.ArrayIndexOutOfBoundsException : length=5; index=5 异常

android - 如何在ListActivity中引用OnListItemClick

android - Github app的进度条是如何实现的

android - 在 ListFragment 中显示 ProgressDialog

android - 在函数内部时,jQuery 更改事件不会在移动设备上触发

android - 如何使用 Native C 在运行时检查操作系统(Linux 或 Android)

Android:切换到 ListActivity 时无法设置 ContentView

java - Android中如何在ActionBarActivity中显示ListFragment?

android - 在 Activity 之间切换黑屏