2

I have a number of arrays stored in Strings.xml file in my android project. I want to populate a list from these Arrays based upon a button click. depends upon which button clicked, i need to populate different lists. So the code is very messy by adding a nested if else structure. Is there any way to get the String Array from the Strings.xml where i can call it using a single common method for all. I am keeping the same name for button tag and array name. please help....`

current code is

if(submit_button.getTag().toString.equalsIgnoreCase("vacation"){
return getResources().getStringArray(R.array.vacation);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("summer"){
return getResources().getStringArray(R.array.summer);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("monsoon"){
return getResources().getStringArray(R.array.monsoon);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("automn"){
return getResources().getStringArray(R.array.automn);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("rainy"){
return getResources().getStringArray(R.array.rainy);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("humid"){
return getResources().getStringArray(R.array.humid);
}
else if(submit_button.getTag().toString.equalsIgnoreCase("sunny"){
return getResources().getStringArray(R.array.sunny);
}
else{
return null;
}

3 Answers 3

3
<string-array name="myarray">
    <item>value1</item>
    <item>value2</item>
    <item>value3</item>
    <item>value4</item>
</string-array>

And in your java code,

String[] MyArray = getResources().getStringArray(R.array.myarray);

I hope this is what you need, If not please let me know.

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

1 Comment

Is there any way to get the String Array from the Strings.xml where i can call it using a single common method for all. That is why I am giving this solution, if not may I know what he is asking exactly.
0

use this method to get the array id from array name..

private int getArrayId(String arranameString) {

    int id = this.getResources().getIdentifier(arranameString, "array", this.getPackageName());

    return id;

} 

This will return the id of your string array

1 Comment

there is no need to parsing it into Long.
0

You use this code;

For string:

public static int getImageResourceIDFromName(String tag,
            Context context) {

        int identifier = context.getResources().getIdentifier (tag,"string",context.getPackageName());
        return identifier;  
    }

For Array:

public static int getImageResourceIDFromName(String tag,
            Context context) {

        int identifier = context.getResources().getIdentifier (tag,"array",context.getPackageName());
        return identifier;  
    }

You need to pass name of identifier then it will return ids of that particular resource it will help you create lots if else statement,

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.