3

I am using an ArrayList and all the strings are hard coded in the adapter but now I need multi-language support for my app. I think the best way to do this is to move all my hard coded strings to my strings.xml file and then make all the string files I need for the different languages.

I already moved the hard coded strings to my strings.xml but I am not sure now how to call them in the Arraylist instead of having the hard coded string in there.

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

List<AdapterData> mItems;

public Adapter() {
super();
mItems = new ArrayList<>();
AdapterData data = new AdapterData();
data.setQuestion("How Old Are You?");
data.setAnswer("I Am 21 Years Old");
mItems.add(data);

data = new AdapterData();
data.setQuestion("Where Were You Born?");
data.setAnswer("I Was Born In The USA");
mItems.add(data);

data = new AdapterData();
data.setQuestion("Are You Male or Female?");
data.setAnswer("I Am Male");
mItems.add(data);



}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
        .inflate(R.layout.recycler_view_card_item, viewGroup, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
AdapterData data = mItems.get(i);
viewHolder.mName.setText(data.getQuestion());
viewHolder.mNameTwo.setText(data.getAnswer());
 }


@Override
public int getItemCount() {

return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

public TextView mQuestion;
public TextView mAnswer;


public ViewHolder(View itemView) {
    super(itemView);
    mQuestion = (TextView)itemView.findViewById(R.id.layoutQuestion);
    mAnswer = (TextView)itemView.findViewById(R.id.layoutAnswer);

}
}

}
Jacques Krause
  • 5,523
  • 9
  • 27
  • 43

6 Answers6

7

Just to provide an alternative, you can also use string-array

<string name="how_old_are_you">How old are you?</string>
<string name="are_you_male_or_female">Are you male or female?</string>
...

<string name="i_am_21">I am 21 years old</string>
<string name="i_was_born_in_the_usa">I was born in the USA</string>
...

<string-array name="questions">
  <item>@string/how_old_are_you</item>
  <item>@string/are_you_male_or_female</item>
  ...
</string-array>

<string-array name="answers">
  <item>@string/i_am_21</item>
  <item>@string/i_was_born_in_the_usa</item>
  ...
</string-array>

To use string-array as a list,

List<String> questions = Arrays.asList(getResources().getStringArray(R.array.questions));

It might come handy.

Fadils
  • 1,508
  • 16
  • 21
5

If you need an array of strings, I think string-array is the best choice. Create a .xml file named arrays.xml and put below code:

<?xml version="1.0" encoding="utf-8"?>
<resources>

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

</resources>

Then get your string arrays:

String[] myStrings = getResources().getStringArray(R.array.array_name); 
Phuc Tran
  • 7,555
  • 1
  • 39
  • 27
2

what you can do is

write below line in the string.xml

 <string name="How_Old_Are_You">How Old Are You?</string>

and in you arraylist

 data.setQuestion(""+getResources().getString(R.string.How_Old_Are_You));

for reference have a look at below link

how to read value from string.xml in android?

another possible solution can be if you create string array in the string.xml that will be easier

 string-array name="tabs_names"> 
    <item>My Tab 1</item> 
    <item>My Tab 2</item>
 </string-array> 

and in you activity write below line to access the array.

 String[] tab_names = getResources().getStringArray(R.array.tab_names);
 String tabname1=tab_names[0];//"My Tab 1"
Community
  • 1
  • 1
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
1

You can get those strings using

String stringOne= getResources().getString(R.string.stringOne);

So using ArrayList, you can do the following:

yourArrayList.add(stringOne)

To get them all at once, you need to name those strings in xml file in some pattern.

Like

string_0
string_1
string_2
.
.

So that your strings.xml file looks like

<resources>
    <string name="string_0">First String</string>
    <string name="string_1">Second String</string>
    <string name="string_2">Third String</string>
</resources>

And then you can use

for (int i = 0; i < numberOfStringsInStringsXML; i++){
int id = getResources().getIdentifier("string_"+i, string, getPackage());
String string= getResources().getString(id);
yourArrayList.add(string);
}

Finally, you will get all the strings into the yourArrayList.

Then you can use the ArrayList to set your data for Adapter.

Nabin
  • 11,216
  • 8
  • 63
  • 98
0

Make a string array resource:

<resources>
    <string-array name="questions">
        <item>@string/question1</item>
        <item>@string/question2</item>
        ...
    </string-array>
</resources>

Make localized versions of your questions. Then in your Activity code you can use

String[] questions = getResources().getStringArray(R.array.questions);

You can either use the String[] directly, or if you need it as a list use Arrays.asList(questions);

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

You can getResources(), getString(string from strings.xml).

So you can do:

mItems.add(getResources().getString(R.string.yourString));
an3
  • 568
  • 1
  • 6
  • 14