如何访问android TypedArray中的第N个字符串数组? 尝试按事件访问TypedArray是行不通的,奇怪的是它似乎不是一个定义的getStringArray()公共方法。
代码如下所示:
//Get array of breed stats by index contained in breed index.
Resources res = getResources();
TypedArray dogBreedInfo = res.obtainTypedArray(R.array.dog_breeds);
String[] selected_breedInfo = dogBreedInfo.get***?????***(breed,0);
Android Developers TypedArray Refrence
<array name="dog_breeds">
<item>
<string-array name="black_russian_terrier">
<item>Working Dogs</item>
<item>2\'2" - 2\'6"</item>
<item>80 - 140 lbs</item>
<item>10 - 11 years</item>
</string-array>
</item>
<item>
<string-array name="boxer">
<item>Working Dogs</item>
<item>1\'9" - 2\'1"</item>
<item>60 - 70 lbs</item>
<item>10 - 12 years</item>
</string-array>
</item>
.
.
.
答案 0 :(得分:1)
你可以试试这个!
getResources().getStringArray(R.id.dog_breeds)[selectedIndex];
- 您可以获得所需的解决方案 - 请告诉我这是否有帮助,或者如果我错了你也可以纠正我。!
答案 1 :(得分:0)
我建议你更改你的xml文件:
对不同的品种信息使用单独的array
:
<array name="Type_of_dog">
<item>black_russian_terrier</item>
<item>boxer</item>
<item>3rd type</item>
<item>4th type</item>
</array>
<array name="characteristic_1">
<item>Working Dogs</item>
<item>Working Dogs</item>
<item>...</item>
<item>...</item>
</array>
<array name="characteristic_2">
<item>2\'2" - 2\'6"</item>
<item>1\'9" - 2\'1"</item>
<item>...</item>
<item>...</item>
</array>
<array name="characteristic_3">
<item>80 - 140 lbs</item>
<item>60 - 70 lbs</item>
<item>...</item>
<item>...</item>
</array>
<array name="characteristic_4">
<item>10 - 11 years</item>
<item>10 - 12 years</item>
<item>...</item>
<item>...</item>
</array>
从Java文件中访问它们:
public void getBreedInfo(int index){
Resources resources = getResources();
TypedArray type = resources.obtainTypedArray(R.array.Type_of_dog);
TypedArray char1 = resources.obtainTypedArray(R.array.characteristic_1);
TypedArray char2 = resources.obtainTypedArray(R.array.characteristic_2);
TypedArray char3 = resources.obtainTypedArray(R.array.characteristic_3);
TypedArray char4 = resources.obtainTypedArray(R.array.characteristic_4);
if(type.equals("black_russian_terrier")) {
// do it something for type 1
} else if(type.equals("boxer") {
// do it something for type 2
}
type.recycle();
char1.recycle();
char2.recycle();
char3.recycle();
char4.recycle();
}
参考答案:this
希望您的代码能够正常运行
答案 2 :(得分:0)
您可以从我的详细答案中提取您的需求。
CharSequence[] infos;
TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.PostableEditText, 0, 0);
try {
infos = a.getTextArray(R.styleable.PostableEditText_infos);
}
finally {
a.recycle();
}
attrs.xml
<declare-styleable name="PostableEditText">
<attr name="infos" format="reference" />
</declare-styleable>
您的custom_layout.xml
<com.my.app.views.custom.edittext.PostableEditText
android:id="@+id/orderProduct"
android:layout_width="match_parent"
android:layout_height="55dp"
app:infos="@array/orderProductInfos" />
ids.xml
<array name="orderProductInfos">
<item>@string/Price</item>
<item>@string/Name</item>
<item>@string/Id</item>
</array>
strings.xml
<!-- Order Product Infos -->
<string name="Price">Price</string>
<string name="Name">Name</string>
<string name="Id">Id</string>
答案 3 :(得分:0)
尝试此操作以从string.xml获取字符串数组
&&