Parth Bhoiwala
Parth Bhoiwala

Reputation: 1322

Android Studio: Get data from XML and store it in a list

I have an XML with the following data:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 one
 two
 three
 four
 five
 six
 seven
 eight
 nine
 ten
</resources>

How do I store that data in an arrayList or something?

Upvotes: 1

Views: 782

Answers (1)

kalabalik
kalabalik

Reputation: 3832

You have to define the type of your data (looks like a bunch of strings to me). Like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="numberstrings">
       <item>one</item>
       <item>two</item>
       ...
    </string-array>
</resources>

Put the data in a file called "strings" (or so) in your res/values folder. Access the data like so:

String[] numberStrings = activity.getResources.getStringArray(R.array.numberstrings);

If you have to, you can then transform the array to a List like so:

List<String> numberStringList = Arrays.asList(numberStrings);

Upvotes: 1

Related Questions