kramer65
kramer65

Reputation: 54013

Android: how to get values from strings.xml into listview?

In my Android app I want to make a list of options that can be clicked. On whichever item you click, it always brings you to the same next screen but with a different .putextra(). I can do this with a simple ListAdapter as is done here.

That works fine, but I do want the values to be translated in the app for different languages. I guess this means I need to define all the values in the listview in my strings.xml. I can of course define some values in strings.xml, but I wouldn't know where to start to get values from my strings.xml into a listview.

Does anybody know how I would be able to get translatable values into an Android ListView? Any tips are welcome!

Upvotes: 1

Views: 1749

Answers (2)

You have two options:

  1. storing strings in strings.xml and make a list or array contains their ids. you can use what @TabrejKhan has said for this option.
  2. Storing a string array as follows and pass it to your adapter.

Like:

<string-array name="my_string_list">
    <item>String 1</item>
    <item>String 2</item>
    <item>String 3</item>
    <item>String 4</item>
    <item>String 5</item>
</string-array>

And in your java code create the adapter

ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(activity,
                R.array.my_string_list);

Upvotes: 5

Tabrej Khan
Tabrej Khan

Reputation: 894

A simple answer is.... Create your array like this:

String[] listItems = {R.string.text1, R.string.text2, R.string.text3, R.string.text4 /*and so on*/ }; 

Upvotes: 0

Related Questions