Search code examples
xamarin.androidandroid-tablelayout

Clickhandler to delete an tablerow


I'm building an app were i add tablerows to a view programmatically. I add a deletebutton to every row that is an ImageButton. Now i have a few questions.

  1. Can i use an ImageButton for this?
  2. How do i get the tablerow id where the deletebutton was clicked?
  3. How can i convert the eventargs from a clickhandler into MenuItemOnMenuItemClickEventArgs or vice versa?
  4. How should my clickhandler look like?

Here is my sourcecode:

public class CalculatorSide2 : Activity
{
    private Button stepButton;
    private IntentHelper intentHelper = new IntentHelper();
    private string age;
    private string estLife;
    private string estPens;
    private string[] pensions;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        intentHelper.IntentSide2(Intent, out age, out estLife,out estPens);

        SetContentView(Resource.Layout.calculatorside2);

        TableLayout tl_layout = FindViewById<TableLayout>(Resource.Id.tableLayout1);

        ImageView plusButton = FindViewById<ImageView>(Resource.Id.plusButton);
        stepButton = FindViewById<Button>(Resource.Id.cside2stepButton);

        plusButton.Click += (sender, e) =>
        {
            PopupMenu popupMenu = new PopupMenu(this, plusButton);
            popupMenu.Inflate(Resource.Menu.popupmenu);
            fillPopupMenu(popupMenu);
            popupMenu.Show();

            popupMenu.MenuItemClick += (s1, arg) =>
            {
                string info = arg.Item.TitleFormatted.ToString();
                string id = arg.Item.ItemId.ToString();

                var inputDialog = new AlertDialog.Builder(this);

                EditText userInput = new EditText(this);
                userInput.InputType = (Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
                inputDialog.SetTitle(info);
                inputDialog.SetView(userInput);

                inputDialog.SetPositiveButton("Ok", (ss, ee) =>
                {
                    TextView rowInfo = new TextView(this);
                    rowInfo.SetLines(2);
                    rowInfo.TextSize = 20;
                    rowInfo.SetTextColor(Android.Graphics.Color.Black);
                    rowInfo.Text = info + ": \n" + userInput.Text + "€";

                    ImageButton delete = new ImageButton(this);
                    delete.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.delete_icon));

                    TableRow row = new TableRow(this);
                    row.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    row.SetBackgroundColor(Android.Graphics.Color.Rgb(255, 153, 0));
                    row.SetPadding(0, 0, 0, 30);

                    row.AddView(rowInfo);
                    row.AddView(delete);
                    tl_layout.AddView(row);
                });
                inputDialog.SetNegativeButton("Cancel", (se, es) => { });

                inputDialog.Show();
            };
        };

        stepButton.Click += (object sender, EventArgs e) =>
        {
            var step = new Intent(this, typeof(CalculatorSide3));
            step.PutExtra("Age", age);
            step.PutExtra("EstPens", estPens);
            step.PutExtra("EstLife", estLife);
            step.PutStringArrayListExtra("Pensions", pensions);
            StartActivity(step);
        };
    }


    private void fillPopupMenu(PopupMenu menu)
    {
        int groupId = 0;
        int i = 0;
        int menuItemId = Android.Views.Menu.First;
        int menuItemOrder = Android.Views.Menu.None;

        foreach (var item in Enum.GetNames(typeof(PopupMenuItems)))
        {
            string itemString = item.ToString();
            menu.Menu.Add(groupId, menuItemId + i, menuItemOrder + i, itemString.Replace("_", " "));
            i++;
        }
    }
}

I hope someone understands what i mean, bc english is not my native language.


Solution

  • public class CalculatorSide2 : Activity
    {
        private Button stepButton;
        private IntentHelper intentHelper = new IntentHelper();
        private string age;
        private string estLife;
        private string estPens;
        private string[] pensions;
        private Dictionary<int,string> temp;
        private TableLayout tl_layout;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            intentHelper.IntentSide2(Intent, out age, out estLife,out estPens);
    
            SetContentView(Resource.Layout.calculatorside2);
    
            tl_layout = FindViewById<TableLayout>(Resource.Id.tableLayout1);
    
            ImageView plusButton = FindViewById<ImageView>(Resource.Id.plusButton);
            stepButton = FindViewById<Button>(Resource.Id.cside2stepButton);
    
            temp = new Dictionary<int,string>();
            int i = 0;
    
            plusButton.Click += (sender, e) =>
            {
                PopupMenu popupMenu = new PopupMenu(this, plusButton);
                popupMenu.Inflate(Resource.Menu.popupmenu);
                fillPopupMenu(popupMenu);
                popupMenu.Show();
    
                popupMenu.MenuItemClick += (s1, arg) =>
                {
                    string info = arg.Item.TitleFormatted.ToString();
                    string id = arg.Item.ItemId.ToString();
    
                    var inputDialog = new AlertDialog.Builder(this);
    
                    EditText userInput = new EditText(this);
                    userInput.InputType = (Android.Text.InputTypes.NumberFlagDecimal | Android.Text.InputTypes.ClassNumber);
                    inputDialog.SetTitle(info);
                    inputDialog.SetView(userInput);
    
                    inputDialog.SetPositiveButton("Ok", (ss, ee) =>
                    {
                        TextView rowInfo = new TextView(this);
                        rowInfo.SetLines(2);
                        rowInfo.TextSize = 20;
                        rowInfo.SetTextColor(Android.Graphics.Color.Black);
                        rowInfo.Text = info + ": \n" + userInput.Text + "€";
    
                        ImageButton delete = new ImageButton(this);
                        delete.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.delete_icon));
                        delete.Focusable = false;
                        delete.Clickable = false;
    
                        TableRow row = new TableRow(this);
                        row.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                        row.SetBackgroundColor(Android.Graphics.Color.Rgb(255, 153, 0));
                        row.SetPadding(0, 0, 0, 30);
                        row.Id = 10 + i;
    
                        row.Click += new EventHandler(HandleClick);
    
                        row.AddView(rowInfo);
                        row.AddView(delete);
                        tl_layout.AddView(row);
    
                        temp.Add(row.Id,userInput.Text);
    
                        i++;
                    });
                    inputDialog.SetNegativeButton("Cancel", (se, es) => { });
    
                    inputDialog.Show();
                };
            };
    
            stepButton.Click += (object sender, EventArgs e) =>
            {
                if (temp.Count != 0)
                {
                    pensions = new string[temp.Count];
                    var j = 0;
                    foreach (KeyValuePair<int, string> pair in temp)
                    {
                        pensions[j] = pair.Value;
                        j++;
                    }
                }
                else
                {
                    pensions = new string[0];
                }
    
                var step = new Intent(this, typeof(CalculatorSide3));
                step.PutExtra("Age", age);
                step.PutExtra("EstPens", estPens);
                step.PutExtra("EstLife", estLife);
                step.PutStringArrayListExtra("Pensions", pensions);
                StartActivity(step);
            };
        }
    
        private void fillPopupMenu(PopupMenu menu)
        {
            int groupId = 0;
            int i = 0;
            int menuItemId = Android.Views.Menu.First;
            int menuItemOrder = Android.Views.Menu.None;
    
            foreach (var item in Enum.GetNames(typeof(PopupMenuItems)))
            {
                string itemString = item.ToString();
                menu.Menu.Add(groupId, menuItemId + i, menuItemOrder + i, itemString.Replace("_", " "));
                i++;
            }
        }
    
        public void HandleClick(object sender, EventArgs e)
        {
            var clickedTR = sender as TableRow;
            int trId = clickedTR.Id;
    
            var builder = new AlertDialog.Builder(this);
            builder.SetTitle("Löschen");
            builder.SetMessage("Möchten Sie den Eintrag wirklich löschen?");
            builder.SetPositiveButton("Ja", (ssr,args) => {
                temp.Remove(trId);
                tl_layout.RemoveView(clickedTR);
            });
            builder.SetNegativeButton("Nein",(sse,arge) => { });
            builder.SetCancelable(false);
            builder.Show();
        }
    }