您需要创建一个 ArrayAdapter,就像填充 ListView 一样。
spinTime = FindViewById<Spinner>(Resource.Id.spinTimeChoose);
List<string> spinnerItems = new List<string>();
ArrayAdapter adapter new ArrayAdapter<string> (this,Android.Resource.Layout.SimpleSpinnerItem,spinnerItems);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinTime.Adapter = adapter;
spinTime.ItemSelected += (s, e) =>
{
Log.Debug("SpinnerItems", spinnerItems[e.Position]);
}
希望能帮助到你
编辑:我想我现在得到了你的问题 :) 下面是一个自定义适配器的示例,用于清除列表。对硬编码的德语字符串感到抱歉。重要的部分是 getView 方法。每当一个新的列表项进入用户视图时,它就会被调用。您必须膨胀一个新视图,或者如果不是 null,则重用 convertView 并用列表中的值填充它。
class ClearanceListAdapter : BaseAdapter
{
private readonly List<Clearance> _clearances;
private readonly Activity _context;
public ClearanceListAdapter(List<Clearance> Clearances, Activity context)
{
_clearances = Clearances;
_context = context;
}
public override int Count
{
get { return _clearances.Count(); }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return position;
}
public Clearance GetClearance (int position)
{
return _clearances.ElementAt(position);
}
public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
{
View view = (convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.ClearanceListItem, parent, false));
Clearance clearance = _clearances[position];
view.FindViewById<TextView>(Resource.Id.txtClearanceDescription).Text = clearance.Description;
view.FindViewById<TextView>(Resource.Id.txtStartDate).Text = clearance.Start.ToString("g");
view.FindViewById<TextView>(Resource.Id.txtEndDate).Text = clearance.End.ToString("g");
return view;
}
}