0

I have the following item.xml -

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
  <TextView
      android:id="@+id/txtTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

  <Button
      android:id="@+id/[id required here for click event.]"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

</LinearLayout>

Using this layout template I am creating multiple items in the code behind -

    public Item(string title, Button btn)
    {
        Text = title;
        Button = btn;
    }

    public override View GetView(int position, View convertView)
    {
        var item = Items[position];
        var view = convertView;

        var contentItem = (Item)item;
        view = _inflater.Inflate(Resource.Layout.ListViewContentItem, null);

        var title = view.FindViewById<TextView>(Resource.Id.txtTitle);
        var btn = view.FindViewById<Button>([button id??]);

        title.Text = contentItem.Text;
        btn = contentItem.Button;

        return view;
    }

My understanding is that id's have to be unique, so how can I create buttons with unique id's so that I can access their click events?

My code would have to be able to handle the creation of multiple Items each with their own buttons.

4
  • Is the button at the end a dynamic button as well or will that one be static Commented Apr 1, 2019 at 10:52
  • And in according to which condition will these show up? Commented Apr 1, 2019 at 12:31
  • @G.hakim sorry I have just realised my xml was confusing I have since amended! Hopefully now you can see each item only require ONE button, however I do not know what id to assign it or how to hookup a click event due to the fact there are going to be multiple items each with thier own button. Commented Apr 1, 2019 at 13:35
  • Check my answer below, let me know if it solves your query! Commented Apr 1, 2019 at 14:03

3 Answers 3

3

That is simple you use id the same way as you used for TextView

So your button would look something like this:

 <Button
  android:id="@+id/buttonId"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

Now in your click event, you can use the Position Parameter to find out which element of the list is receiving the click even at eg:

  var btn = view.FindViewById<Button>(Resource.Id.buttonId);
  btn.Clicked+= (s,e)=>
  {
    if(position==someValue)
    {//Code}
    else
    {//Code}
  };
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I will try this (tonight) and mark as correct once implemented.
Sure no problem, let me know if you face any issue
0

You can write this code in GetView method :

 var btn = view.FindViewById<Button>(Resource.Id.buttonId);
    btn.Tag=position;
    btn.SetOnClickListener(this);

after implement "View.IOnClickListener" in your adapter and override Onclick method

public void OnClick(View v)
        {
            switch (v.Id)
            {
                case Resource.Id.buttonId:
                   //Write code here
                    break;

            }

        }

Comments

0

In your XML:

<Button
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

And for you activity:

Button btn = view.FindViewById<Button>(Resource.Id.btn);
btn.Clicked+= btn_click;

private void LiveLeadSearch_Click(object sender, EventArgs e)
{
   //Your implementation
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.