C# - Fill Spinner with an array from resource file

2019-08-28 19:44发布

问题:

(I am struggling with a simple problem I think!?

In my android app I made a spinner that will be filled with 2 values: On and Off. So I created a file called Arrays.xml in Resources/values/

Now I thought that I need something like this, but this doesn't work:

Spinner ActionSpinner = FindViewById<Spinner>(Resource.Id.ActionSpinner);
ActionSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(ActionSpinner_ItemSelected);
var ActionSpinnerAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.action_array, Android.Resource.Layout.SimpleSpinnerItem);
ActionSpinnerAdapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
ActionSpinner.Adapter = ActionSpinnerAdapter;

The spinner stays empty and I don't know what to do... Someone else asked this question too on this site, but this awnser doesn't seem to work for me. This was his awnser:

ArrayAdapter<String> adp1=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, list);

Any suggestions?