Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#Xamarin AutoCompleteTextView不显示任何内容_C#_Android_Xamarin_Autocompletetextview - Fatal编程技术网

c#Xamarin AutoCompleteTextView不显示任何内容

c#Xamarin AutoCompleteTextView不显示任何内容,c#,android,xamarin,autocompletetextview,C#,Android,Xamarin,Autocompletetextview,在Xamarin Android中,我试图将一个对象绑定到AutocompleteTextView以显示名称/姓氏,但AutocompleteTextView中没有显示任何内容。。(使用ListView而不是AutocompleteTextView,一切正常) 在AutocompleteTextView中,我想显示列表中的球员姓名 [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = tru

在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;
    }

    ..
CustomItem

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

public class Player
{
    public String name;

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

    public string Name
    {
        get { return name; }
    }
}
public class MyHolder
{
    public TextView NameTxt;

    public MyHolder(Android.Views.View itemView)
    {
        NameTxt = itemView.FindViewById<TextView>(Resource.Id.nameTxt);
    }
}
公共类MyHolder
{
公共文本视图名称TXT;
公共MyHolder(Android.Views.View项目视图)
{
NameTxt=itemView.findviewbyd(Resource.Id.NameTxt);
}
}
这里model.xml

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;
    }
}
<?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\u 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>

最简单的方法是使用
ArrayAdapter
直接传入类型为
string
的数组

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;
players=新列表();
玩家。添加(新玩家(“詹姆斯”);
玩家。添加(新玩家(“约翰”);
添加(新玩家(“杰克”);
添加(新玩家(“雨果”);
列表名称列表=新列表();
foreach(玩家中的变量项)
{
名称列表。添加(项目名称);
}
autocomplete=findviewbyd(Resource.Id.autocomplete);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(这个,Resource.Layout.model,Resource.Id.nameTxt,nameList);
autocomplete.Adapter=arrayAdapter;

如果你想通过
列表
,你应该让你的自定义适配器实现
可过滤
接口

它能工作吗?