Adding Progressdialog in app

OmkarOmkar INMember ✭✭

Hello, I'm new to android app development. I want to add progressdialog in my app to display progress bar. I've used async and await method as below, progressdialog gets generated properly but the await task gets stuck near Array Adapter and fails without giving any error. Please help to fix the bug.

async void spinnerProdGroup_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
productGroup = string.Format (this.lstSpinnerProductGroup.ElementAt(e.Position).ID);
productGroupValue = string.Format (this.lstSpinnerProductGroup.ElementAt (e.Position).Value);

        //Spinner spinnerProduct = FindViewById<Spinner> (Resource.Id.spinnerProdType);

        ProgressDialog progress = new ProgressDialog (this);
        progress.Indeterminate = true;
        progress.SetProgressStyle (ProgressDialogStyle.Spinner);
        progress.SetMessage ("Loading.. Please wait!");
        progress.SetCancelable (true);
        progress.Show (); 

            await generateProductGroup(productGroup, productGroupValue, this);

        progress.Hide ();
    }

async Task generateProductGroup(string PGID, string PGValue, Context nea)
{
await Task.Run(() => {
try {
Spinner spinnerProduct = FindViewById (Resource.Id.spinnerProdType);

                System.Data.DataSet dtProduct = new System.Data.DataSet();
                dtProduct = bccd.fetchProdType (PGID);

                lstSpinnerProduct = new List<CItem> ();
                lstSpinnerProduct.Add (new CItem () { ID = "0", Value = "Select Product Type" });
                if(dtProduct.Tables.Count > 0)
                {
                    if(dtProduct.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row in dtProduct.Tables[0].Rows) {
                            lstSpinnerProduct.Add (new CItem () { ID = row [0].ToString (), Value = row [1].ToString () });
                        }
                    }
                }
                ArrayAdapter ProdAdapter = new ArrayAdapter(nea, Resource.Layout.Spinner_Item, lstSpinnerProduct);
                ProdAdapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
                spinnerProduct.Adapter = ProdAdapter;   
            } catch (Exception ex) {
                Toast.MakeText(this,ex.Message,ToastLength.Long).Show();    
            }
        });
    }
Sign In or Register to comment.