gpt4 book ai didi

c# - Android - 使用 bool 值选中 'Custom ListView' 中的复选框

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:02 24 4
gpt4 key购买 nike

我的 C# Android 应用程序中有一个自定义 ListView ,每一行都包含一个 TextView 、ImageView 和一个复选框。单击 Listview 项目时,我想使用 bool 值检查该行的项目复选框。

MainActivity.cs

List<TableList> list = = new List<TableList>();
list.Add(new TableList("Germany"));
list.Add(new TableList("France"));
list.Add(new TableList("Finland"));
listView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs e)
{
string selected = t.Name;
if (selected == "France")
{
Class1.bl = false; // Check the proper checkbox for France row
}
};

Class1.bl is a static public bool set to true

ListView 的 ListAdapter 和 ListClass:

public class ListAdapter : BaseAdapter<TableList>
{
List<TableList> items;
Activity context;
public ListAdapter(Activity context, List<TableList> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableList this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CoinList, null);
view.FindViewById<TextView>(Resource.Id.CoinName).Text = item.Name;
view.FindViewById<ImageView>(Resource.Id.imageView1).SetImageResource(Resource.Drawable.n);

if (Class1.bl == false)
{
view.FindViewById<CheckBox>(Resource.Id.checkBox1).Checked = true;
Class1.bl = true;
}
else
{
view.FindViewById<CheckBox>(Resource.Id.checkBox1).Checked = false;
}
return view;
}
}
public class TableList
{
public string Name;
public TableList(string Name)
{
this.Name = Name;
}
}

当我运行上面的代码时,我选择了法国,ChechBox 被选中用于法国,但是当我选中另一个项目(如德国)时,ChechBox 用于德国是未检查。为什么会这样,我该如何解决?

最佳答案

我已经在您的 TableList 类中添加了 bool:

//I add the bool in this class, so you can change the CheckBox's state by your Data Source
//That means that your CheckBox's state based upon your Data Source.
public class TableList : Java.Lang.Object
{
public TableList(string name, bool b)
{
this.Name = name;
this.bl = b;
}
public string Name { get; set; }
public bool bl { get; set; }
}

我还在 github 上提供了演示, 和 here是结果 gif。

关于c# - Android - 使用 bool 值选中 'Custom ListView' 中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50565878/

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