bharath gangupalli
bharath gangupalli

Reputation: 550

How to add items to a stringArray in an arrayList programmatically?

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?

Upvotes: 5

Views: 24053

Answers (2)

Debarati
Debarati

Reputation: 3286

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);

Upvotes: 11

jeet
jeet

Reputation: 29199

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.

Upvotes: 0

Related Questions