在我的listview适配器类中,我有一个委托来为列表视图中的每个按钮分配一个函数。这个按钮应该在我的"Spices“对象中切换一个名为cupboard
的布尔值。它在第一次单击时起作用,并将其更改为"true“。但它不会再回到false。你知道是什么原因导致它不能再次设置为false吗?这是我在这里的第一篇文章,如果是重复的,我深表歉意。
我尝试将默认值"false“更改为具有getter和setter,并尝试使用不同的if/else语句来切换布尔值。
这来自GetView()
函数中的自定义listView适配器:
Button AddToCupboard = row.FindViewById<Button>(Resource.Id.AddToCupboard);
//Adds onclick event for "Add to cupboard"
AddToCupboard.Click += delegate
{
var item = mItems[position].cupboard;
item = !item;
Console.WriteLine(item);
};
我的Spice
类:
class Spice
{
public string name { get; set; }
public string flavour { get; set; }
public string picture { get; set; }
public string substitute { get; set; }
public string description { get; set; }
public string uses { get; set; }
public bool cupboard { get; set; }
public bool list { get; set; }
}
我期望控制台在同一个按钮按下两次时输出True
,然后输出False
,但它只是停留在true
。
发布于 2019-06-10 05:22:19
这是因为您实际上并没有更改cupboard
对象中的Spice值。
AddToCupboard.Click += delegate
{
var mItem = mItems[position];
mItem.cupboard = !mItem.cupboard;
}
在您的示例中,您可以将cupboard
值复制到一个局部变量中,将其从T切换为F,仅此而已。mItems
数组将保持不变,因为所有操作都是在感兴趣的变量的副本上完成的。
https://stackoverflow.com/questions/56518430
复制相似问题