Xamarin.Android:动态自动完成文本视图

Xamarin.Android: Dynamic AutoCompleteTextView

我创建了一个适配器:

// public
ArrayAdapter<string> adapter { get; set; }
List<string> autocomplete = new List<string>();
// OnCreate()
AutoCompleteTextView autoComplete = FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);
adapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, autocomplete);
autoComplete.Adapter = adapter;
autoComplete.Threshold = 5;

这里我要换适配器或者补充建议

// AfterTextChanged()
adapter.Clear(); // Clear Adapter (previous suggestions)
// Get Autocomplete from Locationiq und Deserialize it
List<AutoComplete> auto = await GetAutoComplete(FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1).Text, Encoding.UTF8.GetString(Convert.FromBase64String(locationiq)));
List<string> temp = new List<string>();
foreach (AutoComplete city in auto)
{
    temp.Add(city.display_place); // Show only the display place (not coordinates etc)
}
autocomplete = temp; // Change the List
adapter.NotifyDataSetChanged(); // Notify that data changed

如果我在使用静态数据的适配器之前创建列表,它工作正常,但我无法让它使用动态数据

Source Code

有人可以帮我解决这个问题吗?我查了很多东西,但我找到了一个可行的解决方案

根据你的描述,你想为AutoCompleteTextView添加动态List,我做了一个简单的例子,你可以看一下:

public class MainActivity : AppCompatActivity
{
    List<string> countries;
    ArrayAdapter adapter;
    AutoCompleteTextView textView;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        countries = new List<string>() {
   "Afghanistan","Albania","Algeria","American Samoa","Andorra",
  "Vanuatu","Vatican City","Venezuela","Vietnam","Wallis and Futuna","Western Sahara",
  "Yemen","Yugoslavia","Zambia","Zimbabwe"
};
        textView = FindViewById<AutoCompleteTextView>(Resource.Id.autocomplete_country);
        adapter = new ArrayAdapter(this, Resource.Layout.list_item, countries) ;

        textView.Adapter = adapter;
        Button btnadd = FindViewById<Button>(Resource.Id.button1);
        btnadd.Click += Btnadd_Click;
        textView.Adapter = adapter;
    }

    private void Btnadd_Click(object sender, EventArgs e)
    {
        countries.Clear();

        countries = new List<string>()
      {
          "chinese","test","english"
      };
        adapter.AddAll(countries);
        adapter.NotifyDataSetChanged();
       


    }