Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling an array from resource file to populate list in android

Tags:

android

I'm working through the listbox building example and wanted to alter it to pull an array from the arrays file (going on the separation of code and data thing)

So instead of declaring the array within the class like so

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 the 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 doesn't seem to get populated with the array

All help appreciated (from a slightly confused beginner programmer)

like image 558
kenwen Avatar asked Nov 10 '11 17:11

kenwen


1 Answers

Try this:

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 android tutorial

like image 82
maddy Avatar answered Nov 15 '22 03:11

maddy