ProgressDialog未在我的ListActivity的onListItemClick方法中显示

时间:2011-06-15 06:44:08

标签: android listactivity progressdialog

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

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

3 个答案:

答案 0 :(得分:2)

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

catalogAdapter.mappingServicesCatalog.loadFromRml(...);

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

您应该使用AsyncTask。首先,您需要将UI处理代码与loadFromRml功能分开。将所有耗时的内容放在doInBackground的{​​{1}}函数中,所有用户界面都应该在AsyncTaskonPreExecute中。

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

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

答案 1 :(得分:0)

我的应用程序中也遇到了相同类型的条件,但我已经使用给定的代码解决了它的代码。

我已经像这样创建了我的进度对话框

final ProgressDialog dialogMatchList = ProgressDialog
            .show(TennisAppActivity.mContext, "",
                    "Loading. Please wait...", true);
    dialogMatchList.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialogMatchList.setIndeterminate(true);
    dialogMatchList.setCancelable(true);

当进度对话框解除打击功能时调用

dialogMatchList.setOnDismissListener(new OnDismissListener() {

        public void onDismiss(DialogInterface arg0) {

            list.setAdapter(new EfficientAdapter(getApplicationContext()));

        }
    });

我希望这是有帮助的,

答案 2 :(得分:0)

    progressDialog = ProgressDialog.show(CatalogActivity.this, "",
                "Please wait for few seconds...", true);

    runOnUIThread(new Runnable()
    {


public void run()
  {
      //Do you task here 
       progressDialog.dismiss();


 }
    });

如果您觉得有用,请不要忘记将其标记为答案

最诚挚的问候,

〜阿努普