8

I'd like to reference a string-array from another string-array in the strings.xml. If I try to read the string-array (named "plants") in my activity the value of every item is null.

Is there a possibility to get these values?

Here is the part of the strings.xml:

<string name="Orlaya_grandiflora_1">Orlaya grandiflora</string>
<string name="Orlaya_grandiflora_2">Large-Flowered Orlaya</string>
<string name="Orlaya_grandiflora_3">Apiaceae</string>
<string-array name="Orlaya_grandiflora">
    <item>@string/Orlaya_grandiflora_1</item>
    <item>@string/Orlaya_grandiflora_2</item>
    <item>@string/Orlaya_grandiflora_3</item>
</string-array>
<string-array name="plants">
    <item>@array/Ginkgo_biloba</item>
    <item>@array/Capsicum_frutescens</item>
    <item>@array/Viscum_album</item>
    <item>@array/Orlaya_grandiflora</item>
</string-array>

I try to access the values like this, for example:

String[] plantArray = resources.getStringArray(R.array.plants);

for (String plant : plantArray) {
        System.out.println("--> " + plant);
    }

The values of plant is in every case "null"

Anybody knows how to access the values?

2
  • what is exacly "resources"? object or else? Commented Jan 4, 2012 at 13:33
  • this was missing: Resources resources = getResources(); Commented Jan 4, 2012 at 13:46

3 Answers 3

21

Straight example given on: Android - String Resources

enter image description here

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

8 Comments

this does not work with references to string-arrays, as I wrote thi side up
Why would you put string arrays inside string arrays? Is that even possible? To me it seems you can only put string resources inside the items and not string-arrays.
@eMich where i have mentioned about putting string arrays inside string arrays?
@eMich and yes i think you may right putting only string resources inside the items.
@Paresh Mayani have u any example ....to read more about string array i have search more and more on stack-over flow i have read your website many questions if u have any example link please send me thank you
|
8

You should do the following :

  //obtain the array that refrences others array
  TypedArray plantArray=getResources().obtainTypedArray(R.array.plants);
  //obtain the referenced arrays
  CharSequence[] ginkoArray=plantArray.getTextArray(0);
  //...
  CharSequence[] orlayaArray=plantArray.getTextArray(3); 

Comments

2
public class Test extends ListActivity {
  String[] presidents;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lstView = getListView();
    // lstView.setChoiceMode(0); //CHOICE_MODE_NONE
    // lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
    lstView.setChoiceMode(2); // CHOICE_MODE_MULTIPLE
    lstView.setTextFilterEnabled(true);

    presidents = getResources().getStringArray(R.array.presidents_array);

    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_checked, presidents));
  }

  public void onListItemClick(ListView parent, View v, int position, long id) {
    parent.setItemChecked(position, parent.isItemChecked(position));

    Toast.makeText(this, "You have selected " + presidents[position],
        Toast.LENGTH_SHORT).show();
  }
};

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">BasicViews5</string>
    <string-array name="presidents_array">
        <item>Dwight D. Eisenhower</item>
        <item>John F. Kennedy</item>
        <item>Lyndon B. Johnson</item>
        <item>Richard Nixon</item>
        <item>Gerald Ford</item>
        <item>Jimmy Carter</item>
        <item>Ronald Reagan</item>
        <item>George H. W. Bush</item>
        <item>Bill Clinton</item>
        <item>George W. Bush</item>
        <item>Barack Obama</item>
    </string-array>
</resources>

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.