Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
.
.
.
like image 215
Bob R Avatar asked Oct 26 '25 02:10

Bob R


2 Answers

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

like image 152
Hardy Avatar answered Oct 27 '25 15:10

Hardy


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

like image 27
Kushal Avatar answered Oct 27 '25 15:10

Kushal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!