简体   繁体   中英

How to add to your array in xml through your java file android?

I have an array of Strings (one item) called "athlete_array" but I want to add to this array through an Activity java file called CreateTeamActivity. Is there a way I can create a method in the CreateTeamActivity class to add to this array? (This code is located in res/values/strings.xml) I might be wrong in thinking there IS a way to do this but if anyone has a similar way to accomplish the same goal I would appreciate it.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Track and Field Team Builder</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_create_team">CreateTeamActivity</string>
    <string-array name="athlete_array">
        <item>Shuttle Badminton</item>
    </string-array>

</resources>

String resources are static and cannot be manipulated by Java code. Instead, you should just use a String[] , or better yet a List<String> . If you need to persist the data, then you will need to use a SQLite database or SharedPreferences .

在您的活动中,致电:

String[] athleteArray = getResources().getStringArray(R.array.athlete_array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM