简体   繁体   English

如何在arraylist中调用strings.xml中的字符串

[英]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. 我使用的是ArrayList并且所有字符串都在适配器中进行了硬编码,但现在我需要为我的应用程序提供多语言支持。 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文件中,然后生成我需要的所有不同语言的字符串文件。

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. 我已经将硬编码字符串移动到我的strings.xml但我现在不确定如何在Arraylist调用它们而不是在那里使用硬编码字符串。

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

}
}

}

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. 它可能会派上用场。

If you need an array of strings, I think string-array is the best choice. 如果你需要一个字符串数组,我认为string-array是最好的选择。 Create a .xml file named arrays.xml and put below code: 创建一个名为arrays.xml的.xml文件并放在下面的代码中:

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

what you can do is 你能做的是

write below line in the string.xml 在string.xml中写下面的行

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

and in you arraylist 在你的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? 如何从android中的string.xml读取值?

another possible solution can be if you create string array in the string.xml that will be easier 另一种可能的解决方案是,如果您在string.xml中创建更容易的字符串数组

 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"

You can get those strings using 你可以使用这些字符串

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

So using ArrayList, you can do the following: 因此,使用ArrayList,您可以执行以下操作:

yourArrayList.add(stringOne)

To get them all at once, you need to name those strings in xml file in some pattern. 要一次性获取它们,您需要以某种模式在xml文件中命名这些字符串。

Like 喜欢

string_0
string_1
string_2
.
.

So that your strings.xml file looks like 这样你的strings.xml文件就像

<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 . 最后,您将获得所有字符串到yourArrayList中

Then you can use the ArrayList to set your data for Adapter. 然后,您可以使用ArrayList为Adapter设置数据。

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); 你可以直接使用String [],或者如果你需要它作为列表使用Arrays.asList(questions);

You can getResources(), getString(string from strings.xml). 你可以使用getResources(),getString(strings.xml中的字符串)。

So you can do: 所以你可以这样做:

mItems.add(getResources().getString(R.string.yourString));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM