我有一个列表视图,当单击ListView上的按钮时,它应该更改ListView外部的TextView文本。
我试过了:
初始化ViewHolder中的TextView
并在主类中具有一个函数,当单击ListView中的按钮时将调用该函数。
无论采用哪种方法,我都会在按钮上获得空引用。
public class ItemOrderActivity : BaseActivity <ItemOrderViewModel>
{
TextView estimatedTotal;
Button continueButton;
ListView itemList;
int count = 0;
static public int[] itemCount = new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Setting layout
SetContentView (Resource.Layout.ItemOrderLayout);
estimatedTotal = FindViewById<TextView> (Resource.Id.estimatedTotal);
// Initializing button view items
continueButton = FindViewById<Button> (Resource.Id.continueButton);
garmentList = FindViewById<ListView> (Resource.Id.itetListView);
ItemListAdapter adapter = new ItemListAdapter(this, getItems());
itemList.Adapter = adapter;
// Button event for OnContinue
continueButton.Click += OnContinue;
}
void total()
{
estimatedTotal.Text = "tet";
}
private class ItemListAdapter : BaseAdapter<Item>
{
private Activity activity;
public List<Item> data;
public ItemListAdapter(Activity activity, List<Item> data)
{
this.activity = activity;
this.data = data;
}
public override Item this[int position]
{
get { return this.data [position]; }
}
public override int Count
{
get { return this.data.Count(); }
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
Holder holder;
holder = new Holder ();
if (convertView == null)
{
convertView = this.activity.LayoutInflater.Inflate (Resource.Layout.ItemLayout, null);
//holder = new Holder ();
holder.Initialize (convertView);
convertView.Tag = holder;
}
//convertView.Tag = holder;
Item item = this.data [position];
holder = (Holder)convertView.Tag;
holder.Bind (this.activity, item.itemName, position);
return convertView;
}
public class Holder : Java.Lang.Object
{
public TextView ItemName;
public Button removeItem;
public Button addItem;
public TextView itemQty;
public int pos;
//TextView estimatedTotal;
public void Initialize(View view)
{
itemName = view.FindViewById<TextView> (Resource.Id.itemTextView);
itemGarment = view.FindViewById<Button> (Resource.Id.removeItemButton);
itemGarment = view.FindViewById<Button> (Resource.Id.addItemButton);
itemQty = view.FindViewById<TextView> (Resource.Id.ItemQtyTextView);
//estimatedTotal = view.FindViewById<TextView> (Resource.Id.estimatedTotal);
addItem.Click += addItemClick;
removeItem.Click += removeItemClick;
}
public void Bind(Activity context, string gn, int position)
{
garmentName.Text = gn;
garmentQty.Text = ItemOrderActivity.ItemCount[position].ToString();
pos = position;
}
public void addButtonClick(object sender, EventArgs e)
{
poa.addItem (pos);
ItemOrderActivity.itemCount [pos]++;
itemQty.Text = ItemOrderActivity.itemCount [pos].ToString ();
//estimatedTotal.Text = "test";
poa.total();
}
}
}
答案 0 :(得分:1)
从按钮获取回调的方式是错误的。 去做这个: 在适配器中创建一个事件以单击按钮。 如果要设置listview适配器,请为更改textview文本的事件添加回调。 在GetView内部或在活页夹中(或绑定视图的任何位置),将事件的处理程序添加到按钮单击事件中。 所以在你的活动中:
protected override void OnCreate (Bundle bundle)
{
//Anything before it
ItemListAdapter adapter = new ItemListAdapter(this, getItems());
adapter.ItemButtonClick += delegate {//Anything you want to do};
itemList.Adapter = adapter;
}
和你的适配器:
private class ItemListAdapter : BaseAdapter<Item>
{
//Anything
public override View GetView(int position, View convertView, ViewGroup parent)
{
//Your code for initializing
if(ItemButtonClick!=null)
theButton.Click += ItemButtonClick;
}
public event EventHandler ItemButtonClick;
}