Jerome Bravin
Jerome Bravin

Reputation: 66

AlertDialog not closing when ok button is clicked- C# xamarin

enter image description here

Hi all having two buttons in a listview . when i click the reject button Alertdialog will come and there i have to enter reason .. after entering the reason on OK click AlertDialog should dismiss .

every thing is saved to DB ..but alert dialog is not closing ... instead after entering three times its closed .. i have attcahed my code below .

      btnReject.Click += delegate
            {
                var currentItem = item;
                Console.WriteLine(position);


                AlertDialog.Builder alert = new AlertDialog.Builder(this.context);
                LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
                View viewdialog = inflater.Inflate(Resource.Layout.RejectJobsAlertDialog, null);
                EditText getreason = viewdialog.FindViewById<EditText>(Resource.Id.Reason);
                alert.SetTitle("Reject Reason ");
                alert.SetView(viewdialog);
                alert.SetPositiveButton("Ok", (senderAlert, args) =>
                {

                    item.RejectedReason = getreason.Text;
                    sharedasproxy.MobileJobdetailsUpdate(item, BASE_URL + "/xxxxx");
                    Toast.MakeText(this.context, "success!", ToastLength.Short).Show();
                    btnReject.Visibility = ViewStates.Invisible;
                    btnAccept.Visibility = ViewStates.Visible;

                    dialog.Cancel();
                });

                alert.SetNegativeButton("Cancel", (senderAlert, args) =>
                {
                    Toast.MakeText(this.context, "Cancelled!", ToastLength.Short).Show();
                    CloseDialog();
                });


                dialog = alert.Create();


                dialog.Show();




            };

Upvotes: 0

Views: 1576

Answers (1)

  • Make sure, the button click event is not attached multiple times.
  • Instead of writing delegate, call a method in the click event.

Try something like below,

 if (!btnReject.HasOnClickListeners)
    {

btnReject.Click += delegate
            {
                var currentItem = item;

                AlertDialog.Builder alert = new AlertDialog.Builder(this.context);
                LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
                View viewdialog = inflater.Inflate(Resource.Layout.RejectJobsAlertDialog, null);
                EditText getreason = viewdialog.FindViewById<EditText>(Resource.Id.Reason);
                alert.SetTitle("Reject Reason ");
                alert.SetView(viewdialog);
                alert.SetPositiveButton("Ok", (senderAlert, args) =>
                {

                    item.RejectedReason = getreason.Text;
                    sharedasproxy.MobileJobdetailsUpdate(item, BASE_URL + "/xxxxx");
                    Toast.MakeText(this.context, "success!", ToastLength.Short).Show();
                    btnReject.Visibility = ViewStates.Invisible;
                    btnAccept.Visibility = ViewStates.Visible;
                    dialog.Cancel();
                });

                alert.SetNegativeButton("Cancel", (senderAlert, args) =>
                {
                    Toast.MakeText(this.context, "Cancelled!", ToastLength.Short).Show();
                    CloseDialog();
                });    

                dialog = alert.Create();
                dialog.Show();


            };
}

Upvotes: 2

Related Questions