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