3

I was wondering if there is anyway to loop through the strings.xml file.

Let's say that I have the following format:

<!-- FIRST SECTION -->
<string name="change_password">Change Password</string>
<string name="change_server">Change URL</string>
<string name="default_password">password</string>
<string name="default_server">http://xxx:8080</string>
<string name="default_username">testPhoneAccount</string>

<!-- SECOND SECTION -->
<string name="debug_settings_category">Debug Settings</string>
<string name="reload_data_every_startup_pref">reload_data_every_startup</string>
<string name="reload_data_on_first_startup_pref">reload_data_on_first_startup</string>

Now let's say I have this:

private HashMap<String,Integer> hashmapStringValues = new HashMap<String, Integer>();

Is there a way to iterate only in the second section of my xml file? Maybe wrap the section with a tag like <section2> and then iterate through it?

public void initHashMap(){
    for (int i=0;i< ???? ;i++) //Here I need to loop only in the second section of my xml file
    {          
        String nameOfTag = ? // Here I get the name of the tag
        int value = R.string.nameOfTag // Here I get the associated value of the tag

        this.hashmapStringValues.put(nameOfTag,value);
    }
}
4
  • 3
    what for ? ... use arrays for that ... or plain xml for settings ... Commented Aug 30, 2012 at 8:46
  • Cause I need to pass each value in a javascript document, and each value has an id in my HTML Form who corresponds to the name tag, so I can't use something like this : <string-array name="my_keys"> <item>Key 1</item> <item>Key 2</item> <item>Key 3</item> <item>Key 4</item> <item>Key 5</item> <item>Key 6</item> </string-array> Commented Aug 30, 2012 at 8:48
  • for one thing, if you put any xml in a folder other than raw folder, the data will not remain as an xml. aapt will generate a compressed more optimized blob which you cannot parse like an xml Commented Aug 30, 2012 at 8:51
  • <item>key|value</item> ... create xml with setting(put it to assets) and use AssetManager.openXmlResourceParser(...) and parse it ... Commented Aug 30, 2012 at 8:51

4 Answers 4

6

No, but you can create another xml file in resources/values which contains this:

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

    <string-array name="array">
        <item>ID_1|Asdf</item>
        <item>ID_2|I do not like to think</item>
        <item>ID_3|Whatever</item>
    </string-array>

</resources>

You can format your strings however you like, and then parse them with a custom parser.

To get the string array you only need to do this:

getResources().getStringArray(R.array.array);
Sign up to request clarification or add additional context in comments.

Comments

5

If you look inside your generated android.R java file, it gives you a good idea how to accomplish this via Reflection:

    Field fields[] = R.string.class.getFields();
    for (Field field : fields) {
        Log.d("appname", field.getName() + ":" + getResources().getIdentifier(field.getName(), "string", this.getPackageName()));
    }

Note, I wouldn't do this often, but once for App load should be fine.

2 Comments

Very helpful : R.string.class.getFields() , thank you!
No problem, it makes me happy that at one point in my life I was useful :)
0

This is not possible, as basic java itself does'nt detect the comments

You can only select the string like this.

getResources().getString(R.string.app_name)

Comments

0
public class MainActivity extends AppCompatActivity{
// one way to iterate over the arrays in the strings.xml file
// is to name them all here......
String[] categories = {"millis","size_uk","size_us","size_jp"};
ArrayList<Spinner> spinners = new ArrayList<>();
ArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // iterate over the views in the layout and find the spinners
    // need to iterate over spinners separately
    // because need to identify them individually
    // in order to set their selection in the event handlers
    // of the other spinners
    GridLayout gridLayout = findViewById(R.id.GridLayout1);
    for (int i = 0; i < gridLayout.getChildCount(); i++) {
        View view = gridLayout.getChildAt(i);
        // if the view is a spinner.....
        if (view instanceof Spinner){
            spinners.add((Spinner)view);
        }
    }
    // it is an array of arrays
    // iterate over the arrays and fill each spinner
    // they used to be called strange objects
    // plus attach an event to each spinner
    for (int i = 0; i < categories.length; i++){
        Spinner spinner = spinners.get(i);
        // identify the array from it's name in the resource file
        int id = this.getResources().getIdentifier(categories[i], "array",
                this.getPackageName());
        // create an adapter from said array
        adapter = ArrayAdapter.createFromResource(this, id,
                android.R.layout.simple_spinner_item);
        //put the contents of the array in the spinner
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        //set listener event for each spinner
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // set all spinners to the same index
                for(int k = 0; k < 4; k++)
                    spinners.get(k).setSelection(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // do nothing
            }
        });
    }
}

}

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.