小编pnk*_*nkj的帖子

如何在Hilt中生成相同类型的对象?

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
    @Singleton
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://example.com/")
            .client(okHttpClient)
            .build()
    }

    @Singleton
    @Provides
    fun provideMyRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http:/my.com/")
            .client(okHttpClient)
            .build()
    }
}
Run Code Online (Sandbox Code Playgroud)

他们的区别仅在于baseUrl


我试图通过使用来解决这个问题@Qualifier

interface RetrofitQualifier {
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class Retrofit

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class MyRetrofit
}
Run Code Online (Sandbox Code Playgroud)
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Singleton
    @Provides
    @RetrofitQualifier.Retrofit
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://example.com/")
            .client(okHttpClient)
            .build()
    }

    @Singleton
    @Provides
    @RetrofitQualifier.MyRetrofit
    fun provideMyRetrofit(okHttpClient: OkHttpClient): …
Run Code Online (Sandbox Code Playgroud)

android kotlin dagger-2 android-jetpack dagger-hilt

12
推荐指数
1
解决办法
4946
查看次数

为什么 paging3 与 Mediator 会无限加载 APPEND

当我通过paging3将数据加载到recyclerview时,数据将被无限加载...... (这意味着我的APPEND将不断执行,并且会不断访问我的网络服务来加载数据)

这是关键代码

Dao is used to operate the StoriesBean

ConstantDao is to operate the latest 日期 that has been stored.

@Dao
interface ConstantDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(bean:ConstantKey)

    @Query("DELETE FROM constant_key")
    suspend fun clear()

    @Query("DELETE FROM constant_key WHERE key = :key")
    suspend fun delete(key: String)

    @Query("SELECT * FROM constant_key WHERE key = :key")
    fun get(key:String):ConstantKey

}
Run Code Online (Sandbox Code Playgroud)
@Dao
interface Dao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(passwords: List<StoriesBean>)

    @Query("DELETE FROM mydata")
    suspend fun clear()

    @Query("SELECT * FROM mydata") …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-jetpack android-paging

5
推荐指数
1
解决办法
1127
查看次数

如何在kotlin中对列表进行分组和合并?

例如,我有以下列表:

data:[
    {
        year:2017,
        price:10
    },
    {
        year:2017,
        price:19
    },
    {
        year:2020,
        price:15
    },
    {
        year:2021,
        price:100
    },
    {
        year:2020,
        price:20
    }
]
Run Code Online (Sandbox Code Playgroud)

我的目的是合并同年的清单价格。如示例列表所示:结果需要是:

data:[
    {
        year:2017,
        price:29
    },
    {
        year:2020,
        price:35
    },
    {
        year:2021,
        price:100
    }
]
Run Code Online (Sandbox Code Playgroud)

有什么办法可以快速实现吗?喜欢groupingbymap...?

java android kotlin

2
推荐指数
1
解决办法
3794
查看次数