[英]How do I avoid hardcoded strings in activity while inputting for recycler view?
I'm working with recycler view
and while inserting String
type text in recycler view I'm using all the hard coded strings and I think it might show problem while translating it into other languages. 我正在使用recycler view
并且在recycler view
插入String
类型的文本时,我正在使用所有硬编码的字符串,并且我认为将其翻译成其他语言时可能会出现问题。
I tried to remove string. 我试图删除字符串。 For example, I removed "about us" and kept R.string.about_us
. 例如,我删除了“关于我们”,并保留了R.string.about_us
。 However, it showed error. 但是,它显示了错误。
wordList.add(new Word(R.drawable.ic_launcher_background, R.string.about_us));
wordList.add(new Word(R.drawable.ic_launcher_background, "Our Facebook Page"));
wordList.add(new Word(R.drawable.ic_launcher_background, "About Us"));
You can't direct access string from string.xml
for you need getResource()
. 您无法从string.xml
直接访问字符串,因为您需要getResource()
。 here is example 这是例子
getResources().getString(R.string.about_us);
Your code should be look like 您的代码应如下所示
wordList.add(new Word(R.drawable.ic_launcher_background, getActivity().getResources().getString(R.string.about_us)));
wordList.add(new Word(R.drawable.ic_launcher_background, "Our Facebook Page"));
wordList.add(new Word(R.drawable.ic_launcher_background, "About Us"));
This worked! 这工作了!
wordList.add(new Word(R.drawable.ic_launcher_background, getString(R.string.about_us)));
wordList.add(new Word(R.drawable.ic_launcher_background, getString(R.string.meme_bazar)));
You have not said the error you got but you need to get the string by calling getString
, do it like this: 您没有说出错误,但是需要通过调用getString
来获取字符串,如下所示:
wordList.add(new Word(R.drawable.ic_launcher_background, context.getString(R.string.about_us)));
wordList.add(new Word(R.drawable.ic_launcher_background, "Our Facebook Page"));
You need to use the values dialog box more often, in your case the one for String
fields. 您需要更频繁地使用“值”对话框,在这种情况下,应使用“ String
字段。 There you can use "New resource" where you enter it's id and value at the same time. 您可以在此处同时输入ID和值的“新资源”。 Then you install the ui that helps you do the translations. 然后,安装可帮助您进行翻译的ui。
For Activity Use, 对于活动用途,
wordList.add(new Word(getResources().getDrawable(R.drawable.ic_launcher_background), getResource().getString(R.string.about_us)));
For Fragment Or Adapter Use, 对于片段或适配器使用,
wordList.add(new Word(context.getResources().getDrawable(R.drawable.ic_launcher_background), context.getResource().getString(R.string.about_us)));
You need to modify your code like below : 您需要修改您的代码,如下所示:
wordList.add(new Word(R.drawable.ic_launcher_background,
getActivity().getString(R.string.about_us));
wordList.add(new Word(R.drawable.ic_launcher_background,
getActivity().getString(R.string.our_facebook_page));
//our_facebook_page = "Our Facebook Page"
wordList.add(new Word(R.drawable.ic_launcher_background,
getActivity().getString(R.string.about_us));
//about_us ="About Us"
You should also put a check like : 您还应该将支票如下:
getActivity() != null
before executing above code to prevent the famous NullPointerException. 在执行上述代码之前,要防止著名的NullPointerException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.