0

I am working with an list containing people. I need to be able to remove or accept a specific person from the list. Each person has a specific ID. I need to know the OwnerID in my function RemovePerson ()

My custom list looks like this.

Name, Button to remove the person, Button to accept the person

James Remove Accept

Jane Remove Accept

Joan Remove Accept

{
    [Activity (Label = "PeopleRequest")]            
    //public class PeopleRequest: Activity
    public class SuperAwesomeActivity : Activity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.PartyRequestLayout);

        var data = new List<SuperAwesomeModel>
        {
            new SuperAwesomeModel {
                OwnerID = 1,
                Name = "James",
            },

            new SuperAwesomeModel {
                OwnerID = 2,
                Name = "Jane",
            },

            new SuperAwesomeModel {
                OwnerID = 3,
                Name = "Joane",
            },

        };

        var listView = FindViewById<ListView>(Resource.Id.PRR_listView);
        listView.Adapter = new SuperAwesomeModelAdapter(this, data);
    }


}

public class SuperAwesomeModel
{
    public int OwnerID { get; set; }
    public string Name { get; set; }
}

My Adapter

public class SuperAwesomeModelAdapter : BaseAdapter<SuperAwesomeModel>
{
    private Button RemoveButton;
    private Button AcceptButton;
    private readonly IList<SuperAwesomeModel> _items;
    private readonly Context _context;

    public SuperAwesomeModelAdapter(Context context, IList<SuperAwesomeModel> items)
    {
        _items = items;
        _context = context;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

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

        if (view == null)
        {
            var inflater = LayoutInflater.FromContext(_context);
            view = inflater.Inflate(Resource.Layout.Row, parent, false);
        }

        view.FindViewById<TextView>(Resource.Id.PRR_Left).Text = item.Name;
        RemoveButton = view.FindViewById<Button> (Resource.Id.PRR_Remove);
        AcceptButton = view.FindViewById<Button> (Resource.Id.PRR_Accept);
        RemoveButton.Click += RemoveButton_Click;
        AcceptButton.Click += AcceptButton_Click;
        return view;
    }


    public override int Count
    {
        get { return _items.Count; }
    }

    public override SuperAwesomeModel this[int position]
    {
        get { return _items[position]; }
    }



    void RemoveButton_Click (object sender, EventArgs e,)
    {

        RemovePerson ();

    }



    void AcceptButton_Click (object sender, EventArgs e)
    {
        AcceptPerson ();


    }



    async void RemovePerson ()
    {

        Toast.MakeText (_context,(String.Format ("Remove: {0} ", _OwnerID )), ToastLength.Short).Show ();

    }

    async void AcceptPerson ()
    {

        Toast.MakeText (_context,"Accept Person", ToastLength.Short).Show ();

    }
}

Row axml

android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/PRR_Left"
    android:layout_height="wrap_content"
    android:layout_width="0px"
    android:layout_weight="1"
    android:text="left" />
<TextView
    android:id="@+id/PRR_Rigtht"
    android:layout_height="wrap_content"
    android:layout_width="0px"
    android:layout_weight="1"
    android:text="right" />
<Button
    android:text="Remove"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/PRR_Remove" />
<Button
    android:text="Accept"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/PRR_Accept" />

Layout axml

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@+id/PRR_listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

1 Answers1

0

instead of calling a class method in your Button Click Event, use a lambda. i.e:

RemoveButton.Click += (s, e) => 
    {
        RemovePerson(item.OwnerID);
    };
Markus Dietrich
  • 618
  • 6
  • 18