Android - Loop Through strings.xml file

Posted by Alexis Cartier on Stack Overflow See other posts from Stack Overflow or by Alexis Cartier
Published on 2012-08-30T08:42:02Z Indexed on 2012/08/30 9:38 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

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);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about android