I have the following problem:
I created a Char Sequence and was able to name 4 units. I would however rather use strings from my XML file for localization purposes. Is there any way to achieve that?
final CharSequence[] choices =
//want to add strings here i.e. R.strings.lemonade
{"Coke", "Pepsi" , "Sprite" , "Seven Up" };
builderType.setSingleChoiceItems( choices, selected, new OnClickListener()
{.......
Error message:
Type mismatch: cannot convert from int to CharSequence
There's another overload of AlertDialog.Builder.setSingleChoiceItems()
that takes in an int
resource id for a string array of items. Put the following in an xml in res/values
e.g. strings.xml
:
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then you can use it as:
builderType.setSingleChoiceItems(R.array.choices, selected, new OnClickListener(), ...
For generic cases, you can also load string array resources with Resources.getStringArray()
as suggested by @Egor.
Create a string-array
in strings.xml
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then fetch it from resources
String[] choices = context.getResources().getStringArray(R.array.choices);
Then use it in setSingleChoiceItems()
as is, since String
implements CharSequence
.
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