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

发布于 2024-11-15 09:25:14 字数 2990 浏览 7 评论 0原文

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

I have ListActivity with BaseAdapter. Each time an user clicks an item I clear the adapter and reload new content (it is some sort of tree logic).

Sometimes it takes several seconds that adapter gets new list of items when in onListItemClick(). I would like to show some progress bar when entering onListItemClick() and hide it when complete.

Why is the progress dialog not showing in my example? I suspect (as I read) the problem would be because the "calculation" is not running in a background thread? If so, can some one please give me an example based on my code?

Here is the code:

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

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

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

发布评论

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

评论(3

腹黑女流氓 2024-11-22 09:25:14

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

catalogAdapter.mappingServicesCatalog.loadFromRml(...);

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

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

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

有很多关于 AsyncTask 的示例,其中之一位于 AsycnTask Android 文档。另一篇有用的文章可以在 Android 开发者资源中找到:Painless Threading

I suspect the busiest line of your code is next:

catalogAdapter.mappingServicesCatalog.loadFromRml(...);

In order for dialog to show you need to return from function that called ProgressDialog.show(...). That's because an actual processing of this call is done in message loop (this loop called your function in the first place). So in your code you're creating a dialog and marking it to be shown, but never really give it a chance to show itself by calling .dismiss() at the end of the same function.

You should use AsyncTask. First you need to separate your UI handling code from your loadFromRml functionality. Put all time consuming thing in doInBackground function of AsyncTask, all UI should be either in onPreExecute or onPostExecute.

You could show progress dialog in onListItemClick function and dismiss it in AsyncTask.onPostExecute function. Or, as an alternative, show progress dialog in AsyncTask.onPreExecute and hide it in AsyncTask.onPostExecute.

There are a lot of examples with AsyncTask, one of which is located in AsycnTask android docs. Another useful article can be found in android developer resources: Painless Threading.

零崎曲识 2024-11-22 09:25:14

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

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

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

        }
    });

我希望它有帮助,

I have also face same type of condition in my application but i have resolved it using the given code the code it is.

I have create my progress dialog like this

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

when progress dialog dismiss the blow function is called

dialogMatchList.setOnDismissListener(new OnDismissListener() {

        public void onDismiss(DialogInterface arg0) {

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

        }
    });

I hope it is help,

神爱温柔 2024-11-22 09:25:14
    progressDialog = ProgressDialog.show(CatalogActivity.this, "",
                "Please wait for few seconds...", true);

    runOnUIThread(new Runnable()
    {


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


 }
    });

如果您发现它有用,请不要忘记将其标记为答案

最好的祝福,

〜Anup

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

    runOnUIThread(new Runnable()
    {


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


 }
    });

If you find it useful please don't forget to mark it as an answer

Best Regards,

~Anup

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