简体   繁体   English

Android-使用布尔值在“自定义ListView”中选中复选框

[英]Android - Check checkbox in 'Custom ListView' using Boolean

I have a custom listview in my C# Android app, each row contains a textview, ImageView and a checkbox. 我在C#Android应用程序中有一个自定义listview,每一行包含一个textview,ImageView和一个复选框。 When a Listview item is clicked, I want to check the row's item checkbox using bool value. 单击Listview项时,我想使用bool值检查该行的项目复选框。

MainActivity.cs 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 Class1.bl是设置为true的静态公共布尔

ListAdapter and ListClass for the Listview: 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;
}
}

The above code when I run it, and I select France, the ChechBox is checked for France but when I check another item like Germany, the ChechBox for Germany is not checked. 当我运行上面的代码并选择“法国”时,会检查ChechBox是否为法国,但是当我检查其他项目(如德国)时,不会选中“德国的ChechBox ”。 Why is this and how can I solve it ? 为什么会这样,我该如何解决?

I have add the bool in your TableList class: 我在您的TableList类中添加了布尔值:

//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; }
}

I also provide the demo on the github , and here is the result gif. 我还在github上提供了演示, 是结果gif。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM