how to set event handler Click for many buttons in xamarin android

Shay Wilner 1,746 Reputation points
2023-06-28T18:11:32.62+00:00

Hello

I display the content of a table in a gridview

I have an xml (grid layout) containing a button and sommes text view .

Also a base adapter class to show a list of record. for each record i display a button

This is what i display

Header 1 Header 2
text text button
text text button |

i can have many rows according to the table

i try to set a event handler click for each button

if i write

private List< Button>  buttons = new List<Button>();
Button abutton;
            for(int i = 0; i < 10; i++) 
            {
               abutton = new Button(this);
                abutton = FindViewById<Button>(Resource.Id.buttons);
                buttons.Add (abutton);
            }
foreach (var item in buttons)
            {
                item.Click += Buttons_Click;
            }

i get an system null reference exception at line item.Click += Buttons_Click;

i think because this line abutton = FindViewById<Button>(Resource.Id.buttons);

Thanks

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Anonymous
    2023-06-30T02:00:09.4466667+00:00

    Hello,

    i can't tell it can be 1 or 5 or10

    Based on your needs, you do not need to reuse items and return the view directly, I removed reuse convertview code like following code.

    The goal is to insert in database data (name birthdate ...) about persons and then displays in gridview and for each row a button and click will show data of the person if i have to update

    If you need to insert items in the Gridview, you can invoke adapter.NotifyDataSetChanged(); method after inserting data to the List<Classdatabirth>.

    If you need to update items in the Gridview when you click item, I write a simple code to update item in the following code.

    public override View GetView(int position, View convertView, ViewGroup parent)
            {       
                var item = listbirthdate[position];
                View view = convertView;
                LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                view = inflater.Inflate(Resource.Layout.tablebirthdate, null);
    
    
               //if (view == null) // otherwise create a new one
                //{
                //    // Create a new ViewHolder object
                   
                //    LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                //    view = inflater.Inflate(Resource.Layout.tablebirthdate, null);   
                //}
                //else
                //{
                  
                //    view = convertView;
                //}
    
                view.FindViewById<TextView>(Resource.Id.tablenamebirth).Text = item.namebirth;
                view.FindViewById<TextView>(Resource.Id.tablesurname).Text = item.surname;
                view.FindViewById<TextView>(Resource.Id.tabledatebirth).Text = item.datebirth;
                view.FindViewById<TextView>(Resource.Id.tableremain).Text = item.remain;
               Button button1 = view.FindViewById<Button>(Resource.Id.button1);
                button1.Click += (o, e) =>
                {
                    //if I need to change the namebirth when I click the button, set it and invoke  NotifyDataSetChanged(); method
                    listbirthdate[position].namebirth="1233333444421";
                    NotifyDataSetChanged();           
                };
                return view;
            }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Shay Wilner 1,746 Reputation points
    2023-06-29T18:10:45.5933333+00:00

    Maybe i found

    In the Class adapter at the

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
         var btn = view.FindViewById<Button>(Resource.Id.buttons);
                btn.Click += Btn_Click;
    private void Btn_Click(object sender, EventArgs e)
            {
                
              }
    

    It'works

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.