ProgressDialog in a ListActivity won't display

JoshuaKrebsJoshuaKrebs Joshua KrebsUSMember
edited May 2013 in Android

I have the following activity which calls out to a service to get some search results. The view works end to end to display search results but for some reason the ProgressDialog never shows (the previous activity just hangs until the data is finished loading). There must be something painfully/stupidly obvious I'm doing wrong here but I can't for the life of me figure out why this won't work. See my code below...

[Activity(Label = "Search Results")]
public class SearchResultsActivity : ListActivity
{
    private PartsClient partsClient;
    private PartsResults results;

    public SearchResultsActivity()
    {
        this.partsClient = new PartsClient("http://192.168.21.100:8080");

    }

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Create your application here

        var progressDialog = ProgressDialog.Show(this, "Please wait...", "Searching...", true);

        var description = Intent.GetStringExtra("Description");

        var searchTask = new Task<PartsResults>(() =>
            {

                var returnObject = partsClient.GetPartsSearchResults(description);
                RunOnUiThread(progressDialog.Dismiss);
                return returnObject;
            });
        searchTask.Start();
        this.results = searchTask.Result;

        this.ListAdapter = new PartsListAdapter(this, results);

    }


    protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        var item = results.Parts[position];
        Toast.MakeText(this, item.ShortDescription, ToastLength.Long).Show();
    }
}

Posts

  • JoshuaKrebsJoshuaKrebs Joshua Krebs USMember

    I should add that the class is inheriting from ListActivity in case it matters...

  • JonathanPryorJonathanPryor Jonathan Pryor USModerator, Xamarin Team Xamurai

    for some reason the ProgressDialog never shows (the previous activity just hangs until the data is finished loading).

    This happens because your SearchResultsActivity.OnCreate() method doesn't return, and thus there isn't anything to show, until after all your data is finished loading.

    Why's that? Because your OnCreate() blocks until all data is available!

    this.results = searchTask.Result;
    

    I would suggest removing the above line, and adjusting searchTask so that the delegate sets this.results.

  • JoshuaKrebsJoshuaKrebs Joshua Krebs USMember
    edited May 2013

    Like I said, it is probably Painfully obvious! :D Thanks! (I call this confessional Debugging)

Sign In or Register to comment.