1

I know how to create a simple Array in xml like this

<resources>
    <string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
    </string-array>
</resources>

And then I initialize it like this in code

ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries_array);
textView.setAdapter(adapter);

But I want to store more items than just a string, so I need a way to store an arraylist rather than a simple array.

Like below

<resources>
<Share>
    <name>Apple</name>
    <currentRate>5.69</currentRate>
    <changeToday>-0.11</changeToday>
    <changeTodayPercent>-0.02</changeTodayPercent>
    <timeUpdated>2012,12,06,18,00,00</timeUpdated>
<Share>
<Share>
    <name>Microsoft</name>
    <currentRate>5.88</currentRate>
    <changeToday>0.19</changeToday>
    <changeTodayPercent>+0.09</changeTodayPercent>
    <timeUpdated>2012,12,06,18,00,00</timeUpdated>
<share>
...
</resources>

I already have a class called Share.java with following variables that need to be set via setMethods

public class ShareHolding {

private String name;
private double currentRate;
private double changeToday;
private double changeTodayPercent;
private GregorianCalendar timeUpdated;
}

and add them to an arraylist

ArrayList<Share> allShares = new ArrayList<Share>();

allShares.getShare(0).setName("I want to access my xml file here and add the first shares name here");
1
  • I'd consider using JSON and save all the shares as a JSON array of JSON objects saved as a single String resource. You would have to create your own way of parsing it however. Commented Dec 6, 2012 at 20:26

1 Answer 1

1

As far as I know, you won't be able to mismatch data types using the resource file. The resource only supports one-way datatypes. e.g Array of string, array of integers, etc.

The best solution that would work is to make the Share xml into it's own file and parse is into your shared object. The android developer has some example of the built in xml parser

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

Comments

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.