Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call strings from strings.xml in arraylist

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

}
}

}
like image 978
Jacques Krause Avatar asked Jul 30 '26 00:07

Jacques Krause


2 Answers

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.

like image 62
Fadils Avatar answered Aug 01 '26 13:08

Fadils


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); 
like image 30
Phuc Tran Avatar answered Aug 01 '26 13:08

Phuc Tran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!