You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从资源获取字符串到Mutable List以实现项目本地化

解决Android本地化资源加载问题

你的问题核心是普通类无法直接调用getString方法——因为getString是Android Context类的成员方法,而你的DummyMateriDataSourceImpl只是普通Kotlin类,没有Context实例,所以直接调用会报错。下面是几种可行的解决方案:

方案1:构造函数注入Context

给DataSource类的构造函数传入Context,通过Context获取资源字符串,同时注意使用ApplicationContext避免内存泄漏:

interface DummyMateriDataSource {
    fun getMateriData(): List<Materi>
}

class DummyMateriDataSourceImpl(private val context: Context) : DummyMateriDataSource {
    override fun getMateriData(): List<Materi> {
        // 切换为ApplicationContext,避免持有Activity/Fragment导致内存泄漏
        val appContext = context.applicationContext
        return mutableListOf(
            Materi(
                imgUrl = "https://raw.githubusercontent.com/ryhanhxx/img_asset/main/img_kalkulator.webp",
                title = appContext.getString(R.string.txt_materi),
                heading = appContext.getString(R.string.txt_materi_heading),
                desc = appContext.getString(R.string.txt_materi_desc),
                subHeading = appContext.getString(R.string.txt_materi_subheading),
                subDesc = appContext.getString(R.string.txt_materi_subdesc),
                source = appContext.getString(R.string.txt_materi_source)
            )
        )
    }
}

使用时,在Android组件(如Activity/Fragment)中传入Context:

// 在Activity中初始化
val dataSource = DummyMateriDataSourceImpl(this)

方案2:依赖注入(以Hilt为例)

如果项目使用依赖注入框架(如Hilt),可以直接注入Resources或ApplicationContext,代码更简洁且避免手动传递Context:

第一步:提供Resources依赖

在Hilt模块中定义Resources的提供方法:

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    fun provideResources(@ApplicationContext context: Context): Resources {
        return context.resources
    }
}

第二步:注入Resources到DataSource

class DummyMateriDataSourceImpl @Inject constructor(private val resources: Resources) : DummyMateriDataSource {
    override fun getMateriData(): List<Materi> {
        return mutableListOf(
            Materi(
                imgUrl = "https://raw.githubusercontent.com/ryhanhxx/img_asset/main/img_kalkulator.webp",
                title = resources.getString(R.string.txt_materi),
                heading = resources.getString(R.string.txt_materi_heading),
                desc = resources.getString(R.string.txt_materi_desc),
                subHeading = resources.getString(R.string.txt_materi_subheading),
                subDesc = resources.getString(R.string.txt_materi_subdesc),
                source = resources.getString(R.string.txt_materi_source)
            )
        )
    }
}

注意事项

  1. 确保资源字符串已在res/values/strings.xml及对应本地化目录中定义,例如:
<resources>
    <string name="txt_materi">本地化标题</string>
    <string name="txt_materi_desc">本地化描述内容</string>
    <!-- 其他对应字段的资源 -->
</resources>
  1. 始终优先使用ApplicationContext而非Activity/Fragment的Context,防止因长期持有组件实例导致内存泄漏。

内容的提问来源于stack exchange,提问作者ryhanhxx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 21:04:55