【问题标题】:Xamarin.Android: Dynamic AutoCompleteTextViewXamarin.Android:动态 AutoCompleteTextView
【发布时间】:2021-04-14 07:02:09
【问题描述】:

我创建了一个适配器:

// 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

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

【问题讨论】:

    标签: c# xamarin xamarin.android android-adapter autocompletetextview


    【解决方案1】:

    根据您的描述,您想为 AutoCompleteTextView 添加动态列表,我创建了一个简单的示例,您可以看看:

    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();
           
    
    
        }
    

    【讨论】:

    • 快速提问。 MSFT 是什么意思?
    • @kaaaxcreators 意思是微软公司。
    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多