gpt4 book ai didi

c# - 带有自定义过滤器的自定义数组适配器

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:50 32 4
gpt4 key购买 nike

我将简要介绍一下我正在尝试做的事情:我想用我自己的对象填充 AutoCompleteTextView 的 DropDown。这些对象包含 3 个字符串。在 list_item_view 中应该是 2 个字符串。此列表应该是可过滤的。

现在是一些代码,我到目前为止所做的:

我的 CustomAdapter 看起来像这样:

public class CustomerAdapter : ArrayAdapter<CustomerSingle>, IFilterable
{
private ws_test.Test ws=null;
public static List<CustomerSingle> _contactList;
private Activity _activity;
private CustomerAdapterFilter filter = null;

public CustomerAdapter(Activity activity, Context context,int resourceId)//List<CustomerSingle> assets)
:base(context,resourceId)//,assets)
{
_activity = activity;
ws=new ws_test.Test();
_contactList = new List<CustomerSingle>();
}

public static List<CustomerSingle> getCustomerList()
{
return _contactList;
}

public void Add(CustomerSingle item)
{
_contactList.Add(item);
}

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

public override long GetItemId(int position)
{
return _contactList[position].id;
}

public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView ?? _activity.LayoutInflater.Inflate(Resource.Layout.details, parent, false);
var contactName = view.FindViewById<TextView>(Resource.Id.Name);
var contactAddress = view.FindViewById<TextView>(Resource.Id.Address);
contactName.Text = _contactList[position].name;// +"\n" + _contactList[position].address;
contactAddress.Text = _contactList[position].address;

return view;
}

public override Filter Filter
{
get
{
return new CustomerAdapterFilter();
}
}

public override void NotifyDataSetChanged()
{
base.NotifyDataSetChanged();
}

public override void NotifyDataSetInvalidated()
{
base.NotifyDataSetInvalidated();
}
}

CustomerSingle 看起来像这样:

public class CustomerSingle
{
public string no { get; set; }
public string name { get; set; }
public string address { get; set; }
public int id { get; set; }

public CustomerSingle(string no, string name, string address, int id)
{
this.address = address;
this.name = name;
this.no = no;
this.id = id;
}
}

好的,现在我需要一个自己的过滤器,我想在这里做的是:

public class CustomerAdapterFilter:Filter
{
protected object mLock = new object();
protected List<CustomerSingle> mOriginalValues = null;

protected override FilterResults PerformFiltering(Java.Lang.ICharSequence prefix)
{

FilterResults results = new FilterResults();
if (mOriginalValues == null) {
lock(mLock) {
mOriginalValues = new List<CustomerSingle>(CustomerAdapter._contactList);

}
}

if (prefix == null || prefix.Length() == 0) {
lock (mLock) {
List<CustomerSingle> list = new List<CustomerSingle>(mOriginalValues);
IntPtr listptr = list.
results.Values = list;
results.Count = list.Count;
}
} else {
String prefixString = prefix.ToString().ToLowerInvariant();

List<CustomerSingle> values = mOriginalValues;
int count = values.Count;

List<CustomerSingle> newValues = new List<CustomerSingle>(count);

for (int i = 0; i < count; i++) {
CustomerSingle value = values.ElementAt(i);
String valueText = value.ToString().ToLowerInvariant();

// First match against the whole, non-splitted value
if (valueText.StartsWith(prefixString)) {
newValues.Add(value);
} else {
String[] words = valueText.Split(' ');
int wordCount = words.Length;

for (int k = 0; k < wordCount; k++) {
if (words[k].StartsWith(prefixString)) {
newValues.Add(value);
break;
}
}
}
}

results.Values = (Object) newValues;
results.Count = newValues.Count;
}

return results;
}

protected override void PublishResults(Java.Lang.ICharSequence constraint, Filter.FilterResults results)
{
//noinspection unchecked
var mObjects = results.Values;
if (results.Count > 0)
{
NotifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
}

我的问题是,我无法从 Java.Lang.Object 转换为我的 CustomerSingle...有人有想法吗?

谢谢!

更新:我在过滤器中更改为 JavaList,并在 CustomerSingle 中对 Java.Lang.Object 进行了扩展

最佳答案

使您的 CustomerSingle 类成为 Java.Lang.Object 的子类:

公共(public)类 CustomerSingle : Java.Lang.Object

更新:

我的猜测是这一行:

results.Values = (Object) newValues;

正在尝试转换为 System.Object 而不是 Java.Lang.Object,请尝试使用 (Java.Lang.Object)。

关于c# - 带有自定义过滤器的自定义数组适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10121581/

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