2 android kotlin android-recyclerview
我需要strings.xml从自定义 Recycler Adapter 类访问字符串,但我收到了如上所述的错误消息。这是我的 Kotlin Recycler Adapter 类,其中包含我尝试使用占位符和onBindViewHolder()访问字符串的方法:milespoundSign
import android.content.res.Resources\nimport android.provider.Settings.Global.getString\nimport android.view.LayoutInflater\nimport android.view.View\nimport android.view.ViewGroup\nimport android.widget.TextView\nimport androidx.recyclerview.widget.RecyclerView\nimport java.text.DecimalFormat\n\nclass SiteAdapter(\n private val mainActivity: CountiesActivity,\n private val siteList: List<Site>) : RecyclerView.Adapter<SiteAdapter.ListItemHolder>() {\n\n inner class ListItemHolder(view: View):\n RecyclerView.ViewHolder(view),\n View.OnClickListener {\n\n internal var name = view.findViewById<View>(R.id.textViewSiteName) as TextView\n\n internal var distance = view.findViewById<View>(R.id.textViewSiteDistance) as TextView\n\n internal var price = view.findViewById<View>(R.id.textViewSitePrice) as TextView\n\n init {\n\n view.isClickable = true\n view.setOnClickListener(this)\n\n }\n\n override fun onClick(view: View) {\n\n mainActivity.showDetails(adapterPosition)\n\n }\n\n }\n\n override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListItemHolder {\n\n val itemView = LayoutInflater.from(parent.context)\n .inflate(R.layout.recycler_list_item, parent, false)\n\n return ListItemHolder(itemView)\n\n }\n\n override fun getItemCount(): Int {\n\n if (siteList != null) {\n return siteList.size\n }\n // error\n return -1\n\n }\n\n override fun onBindViewHolder(holder: ListItemHolder, position: Int) {\n\n val site = siteList[position]\n\n holder.name.text = site.name\n holder.distance.text = site.distance.toString() + Resources.getSystem().getString(R.string.miles)\n val decimalFormatter = DecimalFormat("#,###.00")\n holder.price.text = Resources.getSystem().getString(R.string.poundSign) + decimalFormatter.format(site.price).toString()\n\n }\n\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的strings.xml文件:
<resources>\n <string name="app_name">GoSiteUK</string>\n <string name="title_england">England</string>\n <string name="title_scotland">Scotland</string>\n <string name="title_wales">Wales</string>\n <string name="title_nireland">N.Ireland</string>\n <string name="helpInstructions">\\n\\n\\n\\n\\n\\nSelect from the available countries at the bottom of the screen to display a list of\n counties for that country. Then select a county for which you wish to see campsites, caravan sites or motorhome sites. Select\n a particular site that you wish to travel to and you will be presented with more information about the site. Click on \\"Take Me\n Here\\" and a map will be displayed with navigation instructions and route information to follow to take you to that selected\n site.\\n\\nFor support contact:\\n\\nriverstonetechuk@gmail.com\\n</string>\n <string name="help">Help</string>\n <string name="title_counties">--- Counties ---</string>\n <string name="title_regions">--- Regions ---</string>\n <string name="title_areas">--- Areas ---</string>\n <string name="title_districts">--- Districts ---</string>\n\n <string name="name">Name:</string>\n <string name="distance">Distance:</string>\n <string name="price">Price:</string>\n <string name="miles"> miles</string>\n <string name="poundSign">\xc2\xa3</string>\n</resources>\n\nRun Code Online (Sandbox Code Playgroud)\n\n错误信息如下所示:
\n\n-01-28 16:09:25.229 16740-16740/com.riverstonetech.gositeuk W/ResourceType: No known package when getting value for resource number 0x7f0f0049\n2020-01-28 16:09:25.230 16740-16740/com.riverstonetech.gositeuk D/AndroidRuntime: Shutting down VM\n2020-01-28 16:09:25.231 16740-16740/com.riverstonetech.gositeuk E/AndroidRuntime: FATAL EXCEPTION: main\n Process: com.riverstonetech.gositeuk, PID: 16740\n android.content.res.Resources$NotFoundException: String resource ID #0x7f0f0049\n at android.content.res.Resources.getText(Resources.java:335)\n at android.content.res.Resources.getString(Resources.java:381)\nRun Code Online (Sandbox Code Playgroud)\n\n任何解决这个问题的解决方案都非常感谢。
\n我认为问题是由于访问造成的
Resources.getSystem()
Run Code Online (Sandbox Code Playgroud)
我强烈建议不要这样做。
我这边的建议:将 ListItemHolder 中的视图设置为可访问字段,如下所示
inner class ListItemHolder(val view: View)
Run Code Online (Sandbox Code Playgroud)
然后您可以通过该视图访问字符串资源:
holder.price.text = holder.view.context.getString(R.string.poundSign) + decimalFormatter.format(site.price).toString()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2565 次 |
| 最近记录: |