gpt4 book ai didi

android - 如何在 Kotlin 中使用 `strings.xml` 从 Recycler Adapter 类访问 `getString()` 中的字符串

转载 作者:行者123 更新时间:2023-12-02 13:19:25 24 4
gpt4 key购买 nike

我需要从自定义 Recycler Adapter 类访问 strings.xml 中的字符串,但我收到了一条错误消息,如所述。这是我的 Kotlin Recycler Adapter 类,方法是 onBindViewHolder(),我尝试访问带有占位符 milespoundSign 的字符串:

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\nriverstonetechuk@gmail.com\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 中的 View 设为这样的可访问字段

inner class ListItemHolder(val view: View)

然后您可以通过该 View 访问字符串资源:

holder.price.text = holder.view.context.getString(R.string.poundSign) + decimalFormatter.format(site.price).toString()

关于android - 如何在 Kotlin 中使用 `strings.xml` 从 Recycler Adapter 类访问 `getString()` 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59953156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com