【发布时间】:2018-05-28 11:55:59
【问题描述】:
我的 C# Android 应用程序中有一个自定义列表视图,每行包含一个文本视图、图像视图和一个复选框。单击 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是一个静态公共 bool 设置为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 为德国未被选中。为什么会这样,我该如何解决?
【问题讨论】:
-
我不再使用switch了。我想使用带有布尔值的复选框。 @JoeLv-MSFT
-
是一回事。
-
不,不是。我想用
Boolean,可以用吗? -
是的,因为逻辑是一样的。
标签: c# android visual-studio listview xamarin