4

How does on access the N'th string-array in the android TypedArray? Attempting to access the TypedArray by incidences is not working, and oddly enough it there doesn't seem to be a getStringArray() public method defined.

The code looks like this:

//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>
.
.
.
1
  • Is it possible to extend say getString to handle these types of arrays? Commented Apr 7, 2015 at 4:12

4 Answers 4

3

You can try this!

getResources().getStringArray(R.id.dog_breeds)[selectedIndex];

-and you can get your desired solution -Please let me know if it was helpful or not or you can also correct me if i am wrong.!

Sign up to request clarification or add additional context in comments.

2 Comments

Error:(86, 92) error: array required, but TypedArray found
May not have solved the @Bob's problem, but solved mine. Works fine. Thanks! +1
1

I suggest you to change your xml file a bit like :

use seperate array for different breed info :

      <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>

From Java file, access them :

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();

}

Reference answer : this

Hope you will get your code working

Comments

1

try this for getting string array from string.xml

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

Comments

0

You can extract your needs from my detailed answer..

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>

your 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.