Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android从TypedArray获取字符串数组_Java_Android_Xml_Model View Controller_Typed Arrays - Fatal编程技术网

Java Android从TypedArray获取字符串数组

Java Android从TypedArray获取字符串数组,java,android,xml,model-view-controller,typed-arrays,Java,Android,Xml,Model View Controller,Typed Arrays,如何访问android TypedArray中的第N个字符串数组? 试图通过incidences访问TypedArray是不起作用的,奇怪的是,它似乎没有定义getStringArray()公共方法 代码如下所示: //Get array of breed stats by index contained in breed index. Resources res = getResources(); TypedArray dogBreedInfo = res.obtainTypedArray(R

如何访问android TypedArray中的第N个字符串数组? 试图通过incidences访问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);


工作犬
2\'2" - 2\'6"
80-140磅
10-11年
工作犬
1\'9" - 2\'1"
60-70磅
10-12年
.
.
.
你可以试试这个

getResources().getStringArray(R.id.dog_breeds)[selectedIndex];
-你可以得到你想要的解决方案
-请让我知道它是否有用,或者如果我错了,你也可以纠正我

我建议您将xml文件更改为:

使用单独的
数组
获取不同的品种信息:

      <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>
参考答案:


希望您的代码能够正常工作

您可以从我的详细答案中提取您的需求。

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>
<com.my.app.views.custom.edittext.PostableEditText
    android:id="@+id/orderProduct"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:infos="@array/orderProductInfos" />
<array name="orderProductInfos">
    <item>@string/Price</item>
    <item>@string/Name</item>
    <item>@string/Id</item>
</array>
<!-- Order Product Infos -->
<string name="Price">Price</string>
<string name="Name">Name</string>
<string name="Id">Id</string>

您的自定义布局.xml

<declare-styleable name="PostableEditText">
    <attr name="infos" format="reference" />
</declare-styleable>
<com.my.app.views.custom.edittext.PostableEditText
    android:id="@+id/orderProduct"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:infos="@array/orderProductInfos" />
<array name="orderProductInfos">
    <item>@string/Price</item>
    <item>@string/Name</item>
    <item>@string/Id</item>
</array>
<!-- Order Product Infos -->
<string name="Price">Price</string>
<string name="Name">Name</string>
<string name="Id">Id</string>

ids.xml

<declare-styleable name="PostableEditText">
    <attr name="infos" format="reference" />
</declare-styleable>
<com.my.app.views.custom.edittext.PostableEditText
    android:id="@+id/orderProduct"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:infos="@array/orderProductInfos" />
<array name="orderProductInfos">
    <item>@string/Price</item>
    <item>@string/Name</item>
    <item>@string/Id</item>
</array>
<!-- Order Product Infos -->
<string name="Price">Price</string>
<string name="Name">Name</string>
<string name="Id">Id</string>

@字符串/价格
@字符串/名称
@字符串/Id
strings.xml

<declare-styleable name="PostableEditText">
    <attr name="infos" format="reference" />
</declare-styleable>
<com.my.app.views.custom.edittext.PostableEditText
    android:id="@+id/orderProduct"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    app:infos="@array/orderProductInfos" />
<array name="orderProductInfos">
    <item>@string/Price</item>
    <item>@string/Name</item>
    <item>@string/Id</item>
</array>
<!-- Order Product Infos -->
<string name="Price">Price</string>
<string name="Name">Name</string>
<string name="Id">Id</string>

价格
名称
身份证件

尝试从string.xml获取字符串数组

 String[] hList = getResources().getStringArray(R.array.price_array);

是否可以扩展say getString来处理这些类型的数组?错误:(86,92)错误:需要数组,但TypedArray Found可能没有解决@Bob的问题,但解决了我的问题。很好。谢谢+1.