简体   繁体   中英

How to store multiple string as a single array in String.xml file

I am learning Andoid Development using Android Programming: The Big Nerd Ranch Guide book. In chapter 2, book develops an Quiz app, where it stores questions in string.xml file as follows:

<string name="question_mideast">The Suez Canal connects the Red Sea
and the Indian Ocean?</string>

The problem with this approach is when I have a lot of strings I (say 1000), I can't call each string by name. A better way is maybe storing strings in an array. So that I can call a particular string as string[2][23] (ie, string of chapter 2, text number 23).

  1. Could you tell me how to save strings in String.xml files as array?

  2. Is there any better way to deal when you have lots of strings, in terms of performance.

Store it in arrays.xml instead. Check this example file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="your_string">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <item>D</item>
 </string-array> 
</resources>

Read it in Java Code as.

String[] mTestArray = getResources().getStringArray(R.array.your_string); 

User String-array

<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
</string-array>

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