处理listview.ItemClick中的按钮单击事件

时间:2017-11-21 20:21:31

标签: java android xamarin

我正在使用xamarin,我正在尝试使用内部按钮处理自定义列表视图中的按钮点击事件。如何从我的主Activity中的Listview.ItemClick处理我的Button?

这是我的适配器

    class MyListViewAdapterInventory : BaseAdapter<InventoryPreviewClass>
    {
        public List<InventoryPreviewClass> mitems;
        private Context mContext;
        private int mRowLayout;
        private int[] mAlternatingColors;


        public MyListViewAdapterInventory(Context context, int rowLayout, List<InventoryPreviewClass> items)
        {
            mitems = items;
            mContext = context;
            mRowLayout = rowLayout;
            mAlternatingColors = new int[] { 0xF2F2F2, 0xbfddff };

        }
        public override int Count
        {
            get
            {
                return mitems.Count;
            }
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override InventoryPreviewClass this[int position]
        {
            get
            {
                return mitems[position];
            }

        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {



            View row = convertView;
            if (row == null)
            {
                row = LayoutInflater.From(mContext).Inflate(Resource.Layout.InventoryPreview, null, false);

            }



                row.SetBackgroundColor(GetColorFromInteger(mAlternatingColors[position % mAlternatingColors.Length]));
            TextView txtInventoryID = row.FindViewById<TextView>(Resource.Id.txtInventoryID);
                txtInventoryID.Text = mitems[position].InventoryItemID;
            TextView txtInventoryName = row.FindViewById<TextView>(Resource.Id.txtInventoryName);
                txtInventoryName.Text = mitems[position].InventoryItemName;
            TextView txtInventoryPrice = row.FindViewById<TextView>(Resource.Id.txtInventoryPrice);
                txtInventoryPrice.Text = mitems[position].InventoryItemPrice.Replace(",", ".");
            Button ExtraBtn = row.FindViewById<Button>(Resource.Id.ExtrasBtn);



            return row;

        }
        private Color GetColorFromInteger(int color)
        {
            return Color.Rgb(Color.GetRedComponent(color), Color.GetGreenComponent(color), Color.GetBlueComponent(color));
        }
    }
}

这是我的活动

   db = new SQLiteConnection(dpPath);
    var table = db.Query<InventoryPreviewClass>("select * from InventoryPreviewClass where CategoryID =" + Connection.CategoryID+ "");
    mItems = new List<InventoryPreviewClass>();
    foreach (var item in table)
    {

        mItems.Add(new InventoryPreviewClass() { InventoryItemID = item.InventoryItemID, InventoryItemName = item.InventoryItemName, InventoryItemPrice = item.InventoryItemPrice });


    }
    MyListViewAdapterInventory adapter = new MyListViewAdapterInventory(this, Resource.Layout.InventoryPreview, mItems);
    mlistview.Adapter = adapter;
    mlistview.ItemClick += Mlistview_ItemClick;

private void Mlistview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
  // Here i need to handle my code for button click event
}

我需要当用户按下按钮点击事件来打开另一个活动但是如何在我的Mlistview.ItemClick事件中使用它? Take a look

使用Clicklistener在我的自定义适配器中处理它是更好的方法吗? 有什么例子吗?

以下是我在点击活动中所做的事情。

ExtraBtn.Click += (sender, e) =>
        {
            try
            {
                Connection.InventoryItemID = mitems[position].InventoryItemID;
                Connection.InventoryItemName = mitems[position].InventoryItemName;
                Connection.RetailPrice = mitems[position].InventoryItemPrice;
                Toast toast = Toast.MakeText(mContext, txtInventoryName.Text, ToastLength.Short);
                toast.Show();
                mContext.StartActivity(typeof(ExtrasPreviewMain));
            }
            catch (Exception ex)
            {
                Toast toast = Toast.MakeText(mContext, Convert.ToString(ex), ToastLength.Long);
                toast.Show();
            }

        };

2 个答案:

答案 0 :(得分:1)

在此处设置您的onclick侦听器:

Button ExtraBtn = row.FindViewById<Button>(Resource.Id.ExtrasBtn);
ExtraBtn.Click += (sender, e) => {
   // Perform action on click
};

<强>更新

public override View GetView(int position, View convertView,ViewGroup parent)
        {
          DataViewHolder holder = null;
          Button ExtraBtn ;
            if (convertView == null)
            {
                convertView = LayoutInflater.From(mContext).Inflate(Resource.Layout.InventoryPreview, null, false);
                 holder = new DataViewHolder();
                 holder.txtInventoryID = convertView.FindViewById<TextView>(Resource.Id.txtInventoryID);         
                 holder.txtInventoryName = convertView.FindViewById<TextView>(Resource.Id.txtInventoryName);   
                 holder.txtInventoryPrice = convertView.FindViewById<TextView>(Resource.Id.txtInventoryPrice);
                 ExtraBtn = convertView.FindViewById<Button>(Resource.Id.ExtrasBtn);
                 ExtraBtn.Click += (sender, e) => {
                    // Perform action on click
                   }; 
                    convertView.Tag = holder;
                  ExtraBtn.Tag = position;
            } else {
                ExtraBtn = convertView.FindViewById<ImageView>(Resource.Id.lr_deleteBtn);
                 holder = convertView.Tag as DataViewHolder;
                 ExtraBtn.Tag = position;
            }
           convertView.SetBackgroundColor(GetColorFromInteger(mAlternatingColors[position % mAlternatingColors.Length]));
           holder.txtInventoryID.Text = mitems[position].InventoryItemID;
           holder.txtInventoryName.Text = mitems[position].InventoryItemName;
           holder.txtInventoryPrice.Text = mitems[position].InventoryItemPrice.Replace(",", ".");

            return convertView;

        }

     public class DataViewHolder : Java.Lang.Object
{
    public TextView txtInventoryID { get; set; }
    public TextView txtInventoryName { get; set; }
    public TextView txtInventoryPrice { get; set; }
}

答案 1 :(得分:0)

我认为你的问题是+ =,使用=而不是。

mlistview.ItemClick += Mlistview_ItemClick;

更改为:

mlistview.ItemClick = Mlistview_ItemClick;