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?
By using ArrayList as intermediate storage:Create an ArrayList with the original array, using asList() method. Simply add the required element in the list using add() method. Convert the list to an array using toArray() method.
You can also add an element to String array by using ArrayList as an intermediate data structure. An example is shown below. As shown in the above program, the string array is first converted into an ArrayList using the asList method. Then the new element is added to the ArrayList using the add method.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With