gpt4 book ai didi

c# - Xamarin 自定义多选 ListView

转载 作者:太空狗 更新时间:2023-10-29 16:27:47 25 4
gpt4 key购买 nike

我想知道是否有办法制作一个多选 ListView ,它实际上能够返回选定的索引。我已经能够使用预制的 multiplechoicelistview 适配器来完成它,但我需要能够编辑它的样式。所以我需要一个自定义 ListView 。这是我的oncreate代码a

 protected override void OnCreate(Bundle savedInstanceState)
{

base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Options);
outList = FindViewById<ListView>(Resource.Id.outList);
var btnCheck = FindViewById<ImageButton>(Resource.Id.btnConfirm);
var btnBack = FindViewById<ImageButton>(Resource.Id.btnBack);
for (int i = 0; i < NewProfileVars.LifeStyles.Length; i++)
{
inList.Add(NewProfileVars.LifeStyles[i].Name);
}


//list contents end here

ListViewAdapter adapter = new ListViewAdapter(this, inList);
outList.Adapter = adapter;
outList.ChoiceMode = ChoiceMode.Multiple;
NewProfile main = new NewProfile();
btnCheck.Click += Confirm;
btnBack.Click += Back;


}

这是我的 ListView 适配器代码

    class ListViewAdapter: BaseAdapter<string>
{
public List<string> Items;
public Context Context;

public ListViewAdapter(Context context, List<string> items)
{
Items = items;
Context = context;
}


public override int Count
{
get { return Items.Count; }
}

public override long GetItemId(int position)
{
return position;

}
public override string this[int position]
{
get { return Items[position]; }
}

public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;

if (row == null)
{
row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false);

}
CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName);
txtName.Text = Items[position];
return row;
}
}

我现在需要的是弄清楚确认按钮将如何保存我选择的内容。提前感谢您的帮助。

最佳答案

我看到您在 ListView 中使用了 CheckBox。您可以使用类似这样的方式获取 Checked 的项目:

首先创建一个类来保存您的项目数据和 Checked 状态,我们称其为示例

public class LifeStylesListItem
{
public string Name { get; set; }

public bool IsSelected { get; set; }

public LifeStylesListItem(string name)
{
Name = name;
}
}

然后修改你的ListViewAdapter

添加一个新的私有(private)字段,用于保存 LifeStylesListItem 列表

private List<LifeStylesListItem> _list;

使用构造函数中传递的 Items 初始化列表。

public ListViewAdapter(Context context, List<string> items)
{
Items = items;
_list = new List<LifeStylesListItem>();

//Your are creating a copy of your Items
foreach (var item in items)
{
_list.Add(new LifeStylesListItem(item));
}

Context = context;
}

GetView 方法中订阅 CheckBox 的 CheckedChange 事件。这样,当检查状态发生变化时,您会收到通知。您还需要根据 Item IsSelected 值设置 Checked 属性。当 ListView 将重用您的单元格时,这是必需的。

public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;

if (row == null)
{
row = LayoutInflater.From(Context).Inflate(Resource.Layout.ListBox, null, false);

}
CheckBox txtName = row.FindViewById<CheckBox>(Resource.Id.cbName);
txtName.Text = _list[position].Name;
txtName.Checked = _list[position].IsSelected;

txtName.CheckedChange -= TxtName_CheckedChange;
txtName.CheckedChange += TxtName_CheckedChange;

return row;
}

添加事件处理程序TxtName_CheckedChange 方法

void TxtName_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
//These lines are used to get the position of the control that was clicked
var obj = sender as CheckBox;
var row = obj?.Parent as View;
var parent = row?.Parent as ListView;

if (parent == null)
{
return;
}

var position = parent.GetPositionForView(row);

// Once you have the position you can get the item and change
// its IsSelected
var item = _list[position];
item.IsSelected = e.IsChecked;
}

然后在适配器中添加的最后一个方法是返回所选项目的方法。借助 Linq(需要添加using System.Linq),您可以像这样查询所选项目。

public List<string> GetCheckedItems()
{
return _list
.Where(a => a.IsSelected)
.Select(b => b.Name)
.ToList();
}

现在在您的 Activity 中,您只需在单击“确认”按钮时从 ListViewAdapter 方法调用 GetCheckedItems:

private void Confirm(object sender, EventArgs e)
{
var checkedItems = adapter.GetCheckedItems();
}

请记住将 adapter 更改为 Activity 中的私有(private)字段

private ListViewAdapter adapter;

希望这有帮助。-

关于c# - Xamarin 自定义多选 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45659913/

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