【问题标题】:Android - Check checkbox in 'Custom ListView' using BooleanAndroid - 使用布尔检查“自定义列表视图”中的复选框
【发布时间】: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 为德国未被选中。为什么会这样,我该如何解决?

【问题讨论】:

  • 您好,我在github 上为您提供了一个演示,该演示对我来说效果很好,我提供了一个gif 向您展示结果,我还记录了@987654323 @ 为你。请测试一下,如果有任何问题,请随时问我。
  • 我不再使用switch了。我想使用带有布尔值的复选框。 @JoeLv-MSFT
  • 是一回事。
  • 不,不是。我想用Boolean,可以用吗?
  • 是的,因为逻辑是一样的。

标签: c# android visual-studio listview xamarin


【解决方案1】:

我已经在你的 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上提供了demo,here是结果gif。

【讨论】:

  • 这行不通,因为我将项目添加到 ListView.ItemClick 之外的 ListView 中,并且我想检查 ItemClick 上的 Listview 项目。查看我的代码。
  • 我在ListView.ItemClick 中的`if (cb.Checked)` 处得到Nullexception,我认为这是由GetChildAt(2) 引起的。另外,我尝试了从 0 到 4 的所有数字,但每次都遇到相同的异常。所以我想也许RelativeLayout ll 是原因,因为我有多个布局。
  • 您可以使用for(int i=0;i&lt;) 获取每个孩子,并检查其类型。
  • 我使用了这个var cb = ll.FindViewById&lt;CheckBox&gt;(Resource.Id.checkBox1); 而不是GetChildAt(2),它正在工作!!
  • 我需要重新检查用户上次检查的项目(因此他不需要每次打开应用程序时都检查它们,它们将被存储并在列表视图中检查)。你能帮我这样做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多