gpt4 book ai didi

java - 处理来自 listview.ItemClick 的按钮单击事件

转载 作者:行者123 更新时间:2023-11-30 10:22:19 25 4
gpt4 key购买 nike

我正在使用 xamarin,我正在尝试处理来自自定义 ListView 的按钮单击事件,其中包含按钮。我如何在主 Activity 中处理来自 Listview.ItemClick 的按钮?

这是我的适配器

    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));
}
}
}

这是我的 Activity

   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
}

我需要当用户按下按钮时单击事件来打开另一个 Activity ,但我如何在我的 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();
}

};

最佳答案

在这里设置你的 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; }
}

关于java - 处理来自 listview.ItemClick 的按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47422009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com