Search code examples
xamarin.androidautocompletetextview

Populate AutoCompleteTextView from a remote API


I have a filter dialog on my Xamarin app that'll have an autocompletetextview to give the user a searchable way to find an item.

the issue is that the autocomplete data will be from an API and I'm having a hard time finding a good solution that works.

I'm following this tutorial and a little lost on getting the filtering to work correctly.


Solution

  • you just want to do is set the data from(web api) into your AutoCompleteTextview's Adapter

    A simple example :

    in activity :

    [Activity(Label = "AutoComplextActivity", MainLauncher = true)]
    public class AutoComplextActivity : Activity
    {
        private ArrayAdapter<string> adapter;
    
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your application here
            SetContentView(Resource.Layout.autocomplext_layout);
            AutoCompleteTextView acTextView = (AutoCompleteTextView)FindViewById(Resource.Id.id_autotextView);
            adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line);
            acTextView.Adapter=adapter;
            GetData();                 
        }
    
        private void GetData()
        {
            //get data form web api,for example the data is below
            List<string> data = new List<string>();
            data.Add("beijing1");
            data.Add("beijing2");
            data.Add("beijing3");
            data.Add("shanghai1");
            data.Add("shanghai2");
            data.Add("guangzhou1");
            data.Add("shenzhen");
            data.Add("adadadsgua");
    
            //add data into adapter
            adapter.AddAll(data);
            adapter.NotifyDataSetChanged();
        }
    }