Bob R
Bob R

Reputation: 617

Android get string-array from TypedArray

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

Upvotes: 4

Views: 6369

Answers (4)

Muhammad Haroon
Muhammad Haroon

Reputation: 210

try this for getting string array from string.xml

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

Upvotes: 1

Farukest
Farukest

Reputation: 1518

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>

Upvotes: 0

Kushal
Kushal

Reputation: 8508

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

Upvotes: 1

Hardy
Hardy

Reputation: 2606

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

Upvotes: 3

Related Questions