I posted before regarding this question, but am still struggling. I created a
custom adapter to bind the autocomplete to an array list dynamically - a web
service is called after 2 characters are typed. The code is a combination of
the xamarin content control samples, and another sample on the web (sorry
OP, don't remember where it's from). So in the activity that hosts the
autocomplete textview, I create and bind the adapter, and in the
autocomplete textview's text changed event, I call the web service and bind
the results to the adapter  -adapter.add(myPoco). The service returns data,
but the adapter.count always remains zero, and the custom adapters
PerformFiltering event is never called. Here is the activity code followed
by the custom adapter code. I'm not sure where to put the code to call the
service. It would seem logical to put it in the text changed, but that's not
working:

 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.login);
...

            AutoCompleteTextView act =
FindViewById<AutoCompleteTextView>(Resource.Id.txtEmailCopyToAC);
            
            //ContactAdapter 
            ca = new ContactAdapter(this,
global::Android.Resource.Layout.SimpleDropDownItem1Line);
            act.Adapter = ca;

            //act.Adapter = new ContactAdapter(this,
global::Android.Resource.Layout.SimpleDropDownItem1Line);
            act.TextChanged += act_TextChanged;
        }

        void act_TextChanged(object sender,
global::Android.Text.TextChangedEventArgs e)
        {
            
            if(((AutoCompleteTextView)sender).IsPerformingCompletion)
            {
                return;
            }
            if (e.Text.Count() < 2)
            {
                return;
            }
            

            String searchChars = e.Text.ToString();
            ca.Clear();
            int companyId = 1;
            int acctId = 1;
            var contacts = Gateway.GetCompanyContacts(companyId, acctId);
            if (contacts.Count > 0)
            {
                foreach (var c in contacts)
                {
                    global::Android.Util.Log.Warn("Disc.Android", "record:"
+ c.EmailAddress);
                    ca.Add(c);
                }
                
            }
        }

    }
}




//custom adapter:

using System;
using System.Collections.Generic;
using System.Linq;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using Android.Graphics;

using activeTRAC.DiSCMobile.Model;

namespace activeTRAC.DiSCMobile.Android.Support
{
    //class ContactAdapter<T> : ArrayAdapter<EmailContactItem>,IFilterable
    class ContactAdapter : ArrayAdapter<EmailContactItem>,IFilterable
    {
        LayoutInflater inflater;
        private List<EmailContactItem> _items;
        Filter filter;
        public ContactAdapter(Activity context, int textViewResourceId)//,
List<EmailContactItem> _items)
            : base(context, textViewResourceId)//, _items)
        {
            inflater = context.LayoutInflater;
            filter = new SuggestionsFilter(this);
            _items = new List<EmailContactItem>();
    
        }

        public override int Count
        {
            get { return _items.Count; }
        }
    
        public EmailContactItem GetItem(int position)
        {
            return _items[position];
        }
    
        public override View GetView(int position, View convertView,
ViewGroup parent)
        {
            View view = convertView;
            if (view == null)
                view =
inflater.Inflate(global::Android.Resource.Layout.SimpleDropDownItem1Line,
null);

            view.FindViewById<TextView>(Resource.Id.txtEmailCopyToAC).Text =
_items[position].EmailAddress;

            return view;
        }

        class SuggestionsFilter : Filter
        {
            //ContactAdapter<EmailContactItem> customAdapter;
            ContactAdapter customAdapter;
            public SuggestionsFilter(ContactAdapter adapter)
               // public SuggestionsFilter(ContactAdapter<EmailContactItem>
adapter)
                : base()
            {
                customAdapter = adapter;
            }
            protected override Filter.FilterResults
PerformFiltering(Java.Lang.ICharSequence constraint)
            {
                FilterResults results = new FilterResults();
                if (constraint != null)
                {
                    List<EmailContactItem> data = new
List<EmailContactItem>();
                    try
                    {
                        data = Gateway.GetCompanyContacts(1, 1);
                        
                    }
                    catch (Exception ex)
                    { 
                        //TODO: handle exception
                    }
                    Java.Lang.Object[] matchObjects;
                    matchObjects = new Java.Lang.Object[data.Count];
                    for(int i=0; i<data.Count; i++)
                    {
                        matchObjects[i] = new
Java.Lang.String(data[i].EmailAddress);
                        //TODO: do something with the key
                    }
                    results.Values = matchObjects;
                    results.Count = matchObjects.Count();
                   
                }
                return results;
            }
            protected override void PublishResults(Java.Lang.ICharSequence
constraint, Filter.FilterResults results)
            {
                if (results != null &amp;&amp; results.Count > 0)
                {
                    //customAdapter.notifyDataSetChanged();
                }
                else
                {
                    //customAdapter.notifyDataSetInvalidated();
                }

            }
        }
    }
}



--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/AutoCompleteTextView-con-t-tp5713327.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to