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

标签 android kotlin android-recyclerview

我需要从自定义 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/

相关文章:

android - 在不使用webservice的情况下在android中连接外部数据库

maven - Kotlin Maven 插件中如果为空则跳过

android - RecyclerView - 未调用 onBindViewHolder 时,findViewHolderForPosition 始终返回 null

android - recyclerview 中不正确的线程错误导致的 Realm 访问

android - 纵向/横向变化时蓝牙断开连接

java - Android 应用程序设置未显示

android - 如何接收 Intent。 ACTION_PACKAGE_ADDED Intent 。应用小部件中的 ACTION_PACKAGE_REMOVED?

java - 无法解析 : :truesdk-0. 7-releasePartner

android - 按下项目时打开新 Activity

android - 如何在适配器类中维护 recyclerview 项目的可见性