Pulling an array from a resource file to populate a list in android

I am working on an example of building a list and I want to change it to pull an array from an array file (code and data are separated)

So, instead of declaring an array inside a class, for example

private static final String[] items={"A", "B", "C","D", "E", "F"};

I have a string array in r.arrays.xml called exercises

<resources>
    <array name="exercises">
        <item>Kettlebells - Swing, Two handed</item>
        <item>Kettlebells - Swing, One handed</item>
        <item>Kettlebells - Squat"</item>
        <item>Kettlebells - Deadlift"</item>
        <item>"Kettlebells - Lunge"</item>
        <item>"Kettlebells - Press</item>
    </array>

Replaced existing

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));

with

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, R.array.exercises));

However, the list does not seem to be populated with an array

All help is appreciated (from a slightly confusing novice programmer)

+5
source share
3 answers

Try the following:

String[] exercises = getResources().getStringArray(R.array.countries_array);  
ArrayAdapter<String> exercisesAdapter=new ArrayAdapter<String>(this, R.layout.simple_list_item_1,exercises);
setListAdapter(exercisesAdapter);

Read the tutorial for Android

+16
source

, - , . int - textView . getResources().getStringArray(R.array.exercises); items, ArrayAdapter.

+2

Try using

Resources res = getResources();
setListAdapter(new ArrayAdapter<String>(this, 
               android.R.layout.simple_list_item_1,
               res.getStringArray(R.array.exercises);
+1
source

All Articles