c#Xamarin AutoCompleteTextView不显示任何内容

问题描述 投票:0回答:1

[在Xamarin Android中,我试图将对象绑定到AutocompleteTextView以显示名称/姓氏,但是AutocompleteTextView中什么也不显示。。(使用ListView代替AutocompleteTextView一切正常)进入AutocompleteTextView,我想从列表中显示一个玩家的名字。

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    private AutoCompleteTextView autocomplete;
    private CustomAdapter adapter;
    public List<Player> players;

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

        players = new List<Player>();
        players.Add(new Player("James"));
        players.Add(new Player("John"));
        players.Add(new Player("Jack"));
        players.Add(new Player("Hugo"));

        autocomplete = FindViewById<AutoCompleteTextView>(Resource.Id.autocomplete);
        adapter = new CustomAdapter(this, Resource.Layout.model, players);

        autocomplete.Adapter = adapter;
    }

    ..

CustomAdapter

public class CustomAdapter : ArrayAdapter
{
    private Context c;
    private List<Player> players;
    private int resource;
    private LayoutInflater inflater;

    public CustomAdapter(Context context, int resource, List<Player> objects) : base(context, resource, objects)
    {
        this.c = context;
        this.resource = resource;
        this.players = objects;
    }

    public override Android.Views.View GetView(int position, Android.Views.View convertView, ViewGroup parent)
    {
        if (inflater == null)
        {
            inflater = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);
        }

        if (convertView == null)
        {
            convertView = inflater.Inflate(resource, parent, false);
        }

        MyHolder holder = new MyHolder(convertView);
        holder.NameTxt.Text = players[position].Name;

        return convertView;
    }
}

玩家

public class Player
{
    public String name;

    public Player(string name)
    {
        this.name = name;
    }

    public string Name
    {
        get { return name; }
    }
}

CustomItem

public class CustomItem
{
    public string Heading { get; set; }
}

MyHolder

public class MyHolder
{
    public TextView NameTxt;

    public MyHolder(Android.Views.View itemView)
    {
        NameTxt = itemView.FindViewById<TextView>(Resource.Id.nameTxt);
    }
}

这里model.xml

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:orientation = "horizontal"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent">
    <TextView 
    android:text = "Medium Text"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:id = "@+id/nameTxt"/>
</LinearLayout>

这里activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <AutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/autocomplete" />
 </LinearLayout>
c# android xamarin autocompletetextview
1个回答
0
投票
players = new List<Player>(); players.Add(new Player("James")); players.Add(new Player("John")); players.Add(new Player("Jack")); players.Add(new Player("Hugo")); List<string> nameList = new List<string>(); foreach (var item in players) { nameList.Add(item.Name); } autocomplete = FindViewById<AutoCompleteTextView>(Resource.Id.autocomplete); ArrayAdapter arrayAdapter = new ArrayAdapter(this, Resource.Layout.model, Resource.Id.nameTxt, nameList); autocomplete.Adapter = arrayAdapter;

如果要传递List<Player>,则应让您的自定义适配器实现Filterable接口

© www.soinside.com 2019 - 2024. All rights reserved.