在cutom列表中,如何将特定按钮连接到列表中的特定项目?

时间:2015-04-24 13:02:58

标签: c# android xamarin xamarin.android

我正在处理包含人员的列表。我需要能够从列表中删除或接受特定的人。每个人都有一个特定的ID。我需要知道函数RemovePerson ()

中的OwnerID

我的自定义列表如下所示。

名称,删除人员的按钮,接受此人的按钮

詹姆斯取消接受

简删除接受

Joan删除接受

{
    [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; }
}

我的适配器

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" />

布局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 个答案:

答案 0 :(得分:0)

而不是在Button Click事件中调用类方法,而是使用lambda。 即:

RemoveButton.Click += (s, e) => 
    {
        RemovePerson(item.OwnerID);
    };