52

Below is the code that i made to retrieve the string array item:

String[] menuArray;

@Override
public void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

 // Create an ArrayAdapter that will contain all list items
    ArrayAdapter<String> adapter;

    menuArray = getResources().getStringArray(R.array.menu); 


    for(int i = 0; i < menuArray.length; i++) 
    {
        Button b = new Button(this);
        b.setText(menuArray[i]);
        ll.addView(b);
    }

    this.setContentView(sv);
 }

This is the strings.xml file:

 <string-array name="menu">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        </string-array>

However, the R.array.menu having this issue to compile: As of ADT 14, resource fields cannot be used as switch cases. Invoke this fix to get more information.

3
  • Yeah, i can provide you snapshot from emulator if you want to see. I just skipped the use of variable x along with try catch block as definition of count() is not provided. Commented May 10, 2012 at 12:02
  • Are you using switches with resources ids? check This Commented May 10, 2012 at 12:26
  • i solve it d.. it is just because i move out the app_name from the strings.xml LOL Commented May 10, 2012 at 14:04

2 Answers 2

94

How to retrieve a string array from resources:

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="my_string_array">
        <item>one</item>
        <item>two</item>
        <item>three</item>
    </string-array>

</resources>

Code

String[] stringArray = getResources().getStringArray(R.array.my_string_array);

(The OP already had their question answered but based on the question title, other people may come here looking for this answer.)

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

Comments

8
for(int i = 0;i<menuArray.length; i++) 
{
    Button b = new Button(this);
    b.setText(menuArray[i]);
    ll.addView(b);
}

Delete the below statement

 try {
        x = count();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

and try to give layout height and width to all Layouts..

2 Comments

i have update the code above. But still having the same issue for the R.array.menu.
Take a separate arrays.xml and take string array t<?xml version="1.0" encoding="utf-8"?> <resources> <array name="testArray"> <item>first</item> <item>second</item> <item>third</item> <item>fourth</item> <item>fifth</item> </array> </resources>here

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.