В 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;
}
}
Player
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);
}
}
Здесь модель. 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>