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