0

There are various arrays defined in arrays.xml. I want to read all the arrays at once and store them. Is there any way to do this?

    <string-array name="ArtDesign">
            <item>Layers</item>
            <item>Tracing</item>
    </string-array>

    <string-array name="Beauty">
            <item>Plastic Surgery Simulation</item>
            <item>SeneStock</item>
            <item>PLASTIC SURGERY BEFORE</item>
    </string-array>

4 Answers4

3

For example if you have string array like below:

`

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

`

Then you can achieve in java class like this:

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

So in your case:

String[] artDesign = getResources.getStringArray(R.array.ArtDesign);
String[] beauty = getResources.getStringArray(R.array.Beauty);
Nabin Khatiwada
  • 478
  • 5
  • 15
1

You have to create two arraylists

List<String> artDdesignList = Arrays.asList(getResources().getStringArray(R.array.ArtDesign));

List<String> beautyList = Arrays.asList(getResources().getStringArray(R.array.Beauty));
Pratik
  • 452
  • 4
  • 17
0

When you create an array resource, There is a unique constant generated by android studio to represent that resource which is R.array.*. You need to use reflection to find all the constants in R.array class and retrieve the resources associated with it.

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
0

After much thinking I think we can read the resource file from InputStreamReader and process the text as we want. By this we can make a hashMap and store all the required values in them.

public static HashMap<String, ArrayList<String>> readRawTextFile(Context ctx, int resId)
{
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    StringBuilder text = new StringBuilder();
    ArrayList<String> value = new ArrayList<>();
    HashMap<String,ArrayList<String>> hashMap = new HashMap<>();

    try {
        String key;
        while (( line = buffreader.readLine()) != null) {
            if(line.indexOf("string-array")!=-1){
                key = line; // add logic to refine for the req substring
            } else if(line.indexOf("/string-array"){
                hashMap.add(key, value);
                value = new ArrayList<>();
            } else{
                value.add(line); //add logic to refine for the req string
            }
        }
    } catch (IOException e) {
        return null;
    }
    return hashMap;
}
anothernode
  • 5,100
  • 13
  • 43
  • 62