简体   繁体   English

输入回收者视图时,如何避免活动中的硬编码字符串?

[英]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.

相关问题 我想将一些物品发送到第二个活动的回收站视图。 我怎么能用房间做到这一点 - I want to send some items to second activity's recycler view. How can I do it with Room 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何在同一活动中添加回收站视图和抽屉布局? - How can I add recycler view and drawer layout in same activity? 如何将我的 API JSON 数据显示到回收站视图中 - How do I display my API JSON data into a recycler view 我如何摆脱工具栏和回收站视图之间的这种差距? - How do i get rid of this gap between the toolbar and the recycler view? 如何使用ResourceBundle来避免Java应用程序中的硬编码配置路径? - How do I use ResourceBundle to avoid hardcoded config paths in Java apps? 如何使用“回收者”视图更改活动? - How do I change activities using my recycler view? 如何在 Android Studio 的回收站视图中删除特定文件? - How do I delete a specific file in recycler view of Android Studio? 碎片活动的回收者视图 - Recycler View on Fragment Activity 如何从Recycler View Holder类中删除项目 - How Do I Delete Item from Recycler View Holder Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM