简体   繁体   中英

Android - How to get from string resources all the elements which contains two or more similar words on their names

I am looking about using the Android's resources and i want to get for example from the strings.xml file all the elements that include the word "menu", all of this is for managing the multiple section translations.

My XML is this

<resources>
<string name="app_name">MyApp</string>
<string name="usage_analytics">Show Analytics</string>
<string name="option_resume">Resume</string>
<string name="option_analisys">Analysis</string>
<string name="option_categories">Categories</string>
<string name="option_settings">Settings</string>

and for this i want to get all the elements that contain the "option" word in their name. In my example i want to get option_resume, option_analysis, option_categories and option_settings

In this case i have found advise to make it manually, like GetString(R.string.option_resume), GetString(R.string.option_analisys), GetString(R.string.option_categories), GetString(R.string.option_settings), etc

But for this case i have to make it dynamically obtaining all the items which have a determined word and without a String array resource.

So any idea for this?

But for this case i have to make it dinamically obtaining all the items which have a determinated word

You do not need to do it dynamically. The resources are known at compile time. A new set of resources will not appear magically in your app with a new prefix.

without a String array resource

A string-array resource would be the sensible way of doing it. I am not quite certain why you do not wish to use it. Given your existing strings.xml from above, have an arrays.xml with:

<string-array name="options">
  <string>@string/option_resume</string>
  <string>@string/option_analisys</string>
  <string>@string/option_categories</string>
  <string>@string/option_settings</string>
</string-array>

Then, requesting the options string array at runtime will give you the desired strings.

If your concern is manually maintaining that arrays.xml file, write yourself a bit of code to generate it as part of your build (eg, custom Gradle task, custom Gradle plugin).

The slow and sloppy way of doing this would be to use Java reflection to iterate over all R.string fields for your app's R class, find their names, see which ones match, then use getString() to get the corresponding string values.

I found the solution with help of @CommonsWare

String[] optionsArray = ctx.getResources().getStringArray(R.array.option_strings);

And using that, i made the action i was looking for.

 private String getData(String suffix){

        String data = "option" + suffix;

        String getStringData = this.getResources().getString(
                this.getResources().getIdentifier(data, Const.STRING,
                        this.getPackageName()));

        return getStringData ; 
    }

call this Function and u will get a Specific string. and if u need a list of string then u can use in the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM