1

In Android Studio I have defined a string array as follows in strings.xml:

<string-array name="item_codes">
    <item>FRE</item>
    <item>PAR</item>
    <item>PC</item>
    <item>SUN</item>
    <item>CTRL</item>
</string-array>

When the user runs the app, I would like to be able to add new elements to this array so that all elements are saved for when the user next opens the app.

So:

1) Is there a way in Android Studio to add a new <item> to <string-array> while the app is running?

2) Alternatively, is there another way to save array elements so they can be accessed again upon restarting the app?

Thanks in advance for any help!

XIII-th
  • 4,820
  • 2
  • 30
  • 52
John Voss
  • 107
  • 1
  • 4

1 Answers1

2

There isn't really any way to dynamically add string resources as these are compiled into read-only binary files. So the answer to 1) is in general No.

What you appear to be looking for is a means of persisting the data, this can be done via shared preferences, a file or via a database such as SQLite.

Using a file would be the most complicated as basically both shared preferences and a database ultimately saves the data to a file, so the lower lever level processes are intrinsic to the using shared preferences or a database.

SQLite is likely the preferable database as it's virtually universally built-in to Android Devices (embedded) and unlike many alternatives is client based and so no server is required.

Shared preferences best suit a specific set of items, whilst a database would be advantageous when dealing with variable amounts of data.

There are many tutorials/guides to using both shared preferences and SQLite databases. Using shared preferences is probably the simplest for limited use, as such this may be of interest:- Android Shared preferences example [closed]

MikeT
  • 32,107
  • 13
  • 38
  • 54