2012-04-12 276 views
0

我会给你一个简短的回顾我想要做的事情: 我想用自己的对象填充AutoCompleteTextView的DropDown。这些对象包含3个字符串。在list_item_view中应该是2个字符串。此列表应可过滤。自定义ArrayAdapter自定义过滤器

现在一些代码,我做了什么,直到如今:

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

好,现在我需要一个自己的过滤器我试图在这里做什么: 公共类CustomerAdapterFilter:过滤器 {

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中

+1

你能提供整个代码吗?因为我在做这项工作时遇到了麻烦。 – 2014-01-29 11:03:56

回答

1

让您CustomerSingle类的子类java.lang.Object继承:

public class CustomerSingle : Java.Lang.Object

UPDATE:

我的猜测是,这条线:

results.Values = (Object) newValues;

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

+0

好主意,做到了这一点,但它不会带来所需的效果......我是否必须重写Java.Lang.Object中的某些内容? – Martin 2012-04-12 15:48:25

+0

感谢您的帮助。现在明白了! – Martin 2012-04-13 11:17:51

+1

请您详细说明解决方案... – anz 2013-09-10 12:40:40