0

I just started working with Android Studio and Java overall. I want to read in a file (can be any file format) that stores an array of pairs of String + long. I then want to type that array into a class (assuming that'll just be a simple for loop constructing the elements by what I've read from the XML) - QUestion is more about reading the data in.

I have fiddled around alot with the files in res/values/ but haven't found a way to assign two values to one item. If I could call xml[0].string and xml[0].long, that'd be by far enough for what I'm intending to do.

2 Answers 2

1

You can set two array in the resources to make one at the end

res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="integer_array">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </integer-array>
    <string-array name="string_array">
        <item>string 1</item>
        <item>string 2</item>
        <item>string 3</item>
        <item>string 4</item>
    </string-array>
</resources>

in your class

Resources res = getResources();
int[] ints = res.getIntArray(R.array.integer_array);
String[] strings = res.getStringArray(R.array.string_array);

//add them to your object array
ArrayList<YourObject> list = new ArrayList();

int i = 0;
while(i < ints.length){
    //make sure the two lists are the same size
    list.add(new YourObject(ints[i], strings[i]));
    i++;
}

I'm not sure if you can get long items with this but integer should do the job

Sign up to request clarification or add additional context in comments.

3 Comments

This works well. I can get around not using long for now, i guess. Another question to add up on this - visualizing my idea a bit. I intend to create a default set of Data the user can use to "test" the App (also for me test it in development, but I wanted something I can actually use in Production later on.. not a half assed person). The user can add other values as well later on, how would I store/read that? Some keywords should be enough to help get some Information on the topic.
Well you could setup a sqlite database and make calls to this, it is more flexible than this. Setting up value in xml file is good for simple things like values for a spinner (if the user have to choose between a fixed amout of item in a dropdown list for instance) but to store user data (or other things), you should go for a database (sqlite is built in, but their are other options aswell)
And you can store long in a database
0

Try This, Example: put this in arrays.xml in rec/values/arrays.xml

<string-array name="array_gender">
        <item>Select Gender</item>
        <item>Male</item>
        <item>Female</item>
    </string-array>

Usage

List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.array_gender));

1 Comment

How would the corresponding XML be located and looking? I'm pretty confused by how most of this handles.. or rather spoiled by .json in js i guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.