问题描述
我在导入字符串数组时遇到问题!
I have a problem with importing a string-array!
当我尝试下面的代码时,Spinners 保持为空并且不加载数组字符串值.这是我使用的代码:
When I try the code below, The Spinners stay empty and don't load the array string values. This is my code I use:
//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 导入一个字符串数组......我做错了什么?
I want to import a string-array from Resources/values/String.xml.... What am I doing wrong?
这是 String.xml 文件:
This is the String.xml file:
<?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>
推荐答案
R.array.coin_array
用于纯原生 Android 开发,其中 R
是具有所有资源的所有资源 ID.在 Xamarin.Android 中,R
变为 Resource
,因此请改用 Resource.Array.coin_array
.
R.array.coin_array
is for pure native Android development where R
is the resource class that has all of the resource IDs for all of your resources. In Xamarin.Android R
becomes Resource
, so try Resource.Array.coin_array
instead.
使用资源中的字符串数组填充微调器的工作代码示例:
Working code sample to populate a spinner with a string-array in resources:
布局 AXML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/on_off" />
<Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/on_off"/>
</LinearLayout>
strings.xml:
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Click Me!</string>
<string name="app_name">SpinnerArray</string>
<string name="on_off">On or Off</string>
<string-array name="spinnerArray">
<item>On</item>
<item>Off</item>
</string-array>
</resources>
C# 代码:
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>
(spinner_ItemSelected);
var spinnerAdapter = ArrayAdapter.CreateFromResource
(this, Resource.Array.spinnerArray,
Android.Resource.Layout.SimpleSpinnerItem);
spinnerAdapter.SetDropDownViewResource
(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = spinnerAdapter;
我已验证上述方法可以从资源中的字符串数组填充微调器.
I have verified that the above works to populate a spinner from a string-array in resources.
这篇关于如何从资源导入字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!