开发者

ProgressDialog is not showing in my ListActivity's onListItemClick method

开发者 https://www.devze.com 2023-03-13 13:51 出处:网络
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).

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;
    RRmlMappingS开发者_运维问答ervicesCatalogItem 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 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.


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,


    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

0

精彩评论

暂无评论...
验证码 换一张
取 消