如何从arraylist中的strings.xml调用字符串 [英] How to call strings from strings.xml in arraylist

查看:23
本文介绍了如何从arraylist中的strings.xml调用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ArrayList 并且所有字符串都在适配器中硬编码,但现在我的应用程序需要多语言支持.我认为最好的方法是将我所有的硬编码字符串移动到我的 strings.xml 文件中,然后制作不同语言所需的所有字符串文件.

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.

我已经将硬编码字符串移到了我的 strings.xml 中,但我现在不确定如何在 Arraylist 中调用它们,而不是将硬编码字符串放在那里.

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

}
}

}

推荐答案

只是为了提供一个替代方案,你也可以使用 string-array

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

它可能会派上用场.

这篇关于如何从arraylist中的strings.xml调用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆