Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to know how to create an array of strings inside strings.xml, and how to access it in java code. I need this in order to use the setText(). Please help.

share|improve this question

closed as off-topic by Pankaj Kumar, Uwe Plonus, nsgulliver, falsetru, Guillaume Jul 24 '13 at 12:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Pankaj Kumar, Uwe Plonus, nsgulliver, falsetru, Guillaume
If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

2 Answers

up vote 4 down vote accepted

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
<string-array name="planets_array"> // name of the string array
    <item>Mercury</item> // items
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
</string-array>
</resources>

In your activity class.

  Resources res = getResources(); 
  String[] planets = res.getStringArray(R.array.planets_array);

                 or

  String[] planets = getResources().getStringArray(R.array.planets_array);

getResource requires activity context. If its in a non activity class you will need the activity context which can be passed to the non activity class contructor.

Source @

http://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Edit:

  new CustomAdapter(ActivityName.this);

Then

  class CustomAdapter extends BaseAdapter
  {
      Context mContext;
      public CustomAdapter(Context context)
      {
           mContext = context;
      }
   ...// rest of the code
  }

use mContext.getResources()

share|improve this answer
    
Thanks for your answer . I need i more thing . I want to do this inside a BaseAdapter which doesnt support getResources();. The way i do this is by calling the getResourses in the MainActivity and then send the array with SharedPreferences to the BaseAdapter . Is there a way to do it directrly in the BaseAdapter ? –  mremremre1 Jul 19 '13 at 12:52
    
@mremremre1 check the edit –  Raghunandan Jul 19 '13 at 12:56
add comment

You create a String array in strings.xml like such:

<string-array name="my_string_array">
      <item>first string</item>
      <item>second</item>
</string-array>

And then you can access it with the following:

String[] str = getResources().getStringArray(R.array.my_string_array);
share|improve this answer
    
Thanks for your answer . I need i more thing . I want to do this inside a BaseAdapter which doesnt support getResources();. The way i do this is by calling the getResourses in the MainActivity and then send the array with SharedPreferences to the BaseAdapter . Is there a way to do it directrly in the BaseAdapter ? –  mremremre1 Jul 19 '13 at 12:52
1  
Yes use your Activty's Context. –  Ruben Weerts Jul 19 '13 at 13:42
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.