5

This is my array.xml file in the res/values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="updateInterval">
           <item name="1000">Pico TTS</item>
          <item name="5000">Invox TTs</item>
    </string-array>
</resources>

I need to add some more items to the updateInterval array list. How can I add the items that are dynamically coming from server programmatically?

1
  • ...and one more thought. Consider maintaining a SQLite DB with new data coming from the server instead of a string array. Commented Jan 10, 2012 at 6:56

2 Answers 2

11

You can't add item directly to that string array.

But you can use that array and dynamically add elements to that string array.

Do in this way.

    String[] array = getResources().getStringArray(R.array.updateInterval);
    System.out.println("--array.length--"+array.length);
    List<String> list = new ArrayList<String>();
    list = Arrays.asList(array);
    ArrayList<String> arrayList = new ArrayList<String>(list);
    arrayList.add("TTS");
    array = arrayList.toArray(new String[list.size()]);
    System.out.println("--array.length--"+array.length);
Sign up to request clarification or add additional context in comments.

3 Comments

how does this add an item to "undateInterval" ?
He is showing you to create a new array based on the old one, then you can add things to it via the ArrayList and Array types.
so how would you then save this back to sharedpreferences?
0

It is not possible to edit an xml resource dynamically, you just can have an static arraylist in case you need some data at runtime plus xml data.

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.