如何从资源中导入字符串数组?

我在导入字符串数组时遇到问题!


当我尝试下面的代码时, Spinners 保持为空并且不加载数组字符串值。这是我使用的代码:


//Fill CoinSpinner

Spinner CoinSpinner = FindViewById<Spinner>(Resource.Id.CoinSpinner);

CoinSpinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(CoinSpinner_ItemSelected);

ArrayAdapter<String> CoinSpinnerAdapter = new ArrayAdapter<String>(this, Resource.Array.coin_array, Android.Resource.Layout.SimpleSpinnerItem);

//ArrayAdapter CoinSpinnerAdapter = ArrayAdapter.CreateFromResource(this, Resource.Array.coin_array, Android.Resource.Layout.SimpleSpinnerItem);

CoinSpinnerAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);

CoinSpinner.Adapter = CoinSpinnerAdapter;

我想从 Resources/values/String.xml 导入一个字符串数组。我做错了什么?


编辑:

这是 String.xml 文件:


<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">TestApp</string>

    <string name="coins_prompt">Coin:</string>

    <string-array name="coin_array">

        <item>EUR</item>

        <item>USD</item>

    </string-array>

</resources>


POPMUISE
浏览 121回答 1
1回答

12345678_0001

R.array.coin_array用于纯原生 Android 开发,其中R包含所有资源的所有资源 ID 的资源类。在 Xamarin.Android 中R变为Resource,因此请尝试Resource.Array.coin_array改用。编辑:使用资源中的字符串数组填充微调器的工作代码示例:布局 AXML:<?xml version="1.0" encoding="utf-8"?><LinearLayout&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp;&nbsp; &nbsp; android:orientation="vertical"&nbsp;&nbsp; &nbsp; android:layout_width="match_parent"&nbsp;&nbsp; &nbsp; android:layout_height="match_parent">&nbsp; &nbsp;<Button android:id="@+id/myButton"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:text="@string/hello" />&nbsp; &nbsp; <TextView android:id="@+id/textView"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/on_off" />&nbsp; &nbsp; <Spinner android:id="@+id/spinner"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;android:prompt="@string/on_off"/></LinearLayout>字符串.xml:<?xml version="1.0" encoding="utf-8"?><resources>&nbsp; &nbsp;<string name="hello">Hello World, Click Me!</string>&nbsp; &nbsp;<string name="app_name">SpinnerArray</string>&nbsp; &nbsp;<string name="on_off">On or Off</string>&nbsp; &nbsp;<string-array name="spinnerArray">&nbsp; &nbsp; &nbsp; <item>On</item>&nbsp; &nbsp; &nbsp; <item>Off</item>&nbsp; &nbsp;</string-array></resources>C#代码:Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>&nbsp; &nbsp; &nbsp; &nbsp; (spinner_ItemSelected);var spinnerAdapter = ArrayAdapter.CreateFromResource&nbsp; &nbsp; &nbsp; &nbsp; (this, Resource.Array.spinnerArray,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Android.Resource.Layout.SimpleSpinnerItem);spinnerAdapter.SetDropDownViewResource&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (Android.Resource.Layout.SimpleSpinnerDropDownItem);spinner.Adapter = spinnerAdapter;我已经验证了上述方法可以从资源中的字符串数组填充微调器。
打开App,查看更多内容
随时随地看视频慕课网APP