gpt4 book ai didi

android-studio - 相同的值作为 nextKey 在从 Paging Library 3 Android 中的 PagingSource 加载的两个连续页面中传递

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

我从分页 2 迁移到分页 3。我尝试将分页 2 的 ItemKeyedDataSource 实现到分页库 3。但我面临的问题是,在加载的两个连续页面中,相同的值(currentJodId)作为 nextkey 传递。在该应用程序崩溃之后。但是如果我在数据源中添加“keyReuseSupported = true”,应用程序不会崩溃。但它开始调用与 nextkey 相同的项目 ID。JobSliderRestApi.kt

@GET("job/list/slides")
fun getDetailOfSelectedJob(
@Query("current_job") currentJodId: Int?,
@Query("limit") jobLimit: Int?,
@Query("search_in") fetchType: String?
): Single<Response<JobViewResponse>>
JobViewResponse.kt
data class JobViewResponse(
@SerializedName("data") val data: ArrayList<JobDetail>?
) : BaseResponse()
JobDetail.kt
data class JobDetail(
@SerializedName("job_id") val jobId: Int,
@SerializedName("tuition_type") val jobType: String?,
@SerializedName("class_image") val jobImage: String,
@SerializedName("salary") val salary: String,
@SerializedName("no_of_student") val noOfStudent: Int,
@SerializedName("student_gender") val studentGender: String,
@SerializedName("tutor_gender") val preferredTutor: String,
@SerializedName("days_per_week") val daysPerWeek: String?,
@SerializedName("other_req") val otherReq: String?,
@SerializedName("latitude") val latitude: Double?,
@SerializedName("longitude") val longitude: Double?,
@SerializedName("area") val area: String,
@SerializedName("tutoring_time") val tutoringTime: String?,
@SerializedName("posted_date") val postedDate: String?,
@SerializedName("subjects") val subjects: String,
@SerializedName("title") val title: String
)
JodSliderDataSource.kt
class JodSliderDataSource @Inject constructor(
private val jobSliderRestApi: JobSliderRestApi
): RxPagingSource<Int, JobDetail>() {

// override val keyReuseSupported = true

@ExperimentalPagingApi
override fun getRefreshKey(state: PagingState<Int, JobDetail>): Int? {
return state.anchorPosition?.let {
state.closestItemToPosition(it)?.jobId
}
}

override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, JobDetail>> {
return jobSliderRestApi.getDetailOfSelectedJob(42673, 2, "next").toSingle()
.subscribeOn(Schedulers.io())
.map { jobResponse -> toLoadResult(jobResponse.data) }
.onErrorReturn { LoadResult.Error(it) }
}

private fun toLoadResult(data: ArrayList<JobDetail>): LoadResult<Int, JobDetail> {
return LoadResult.Page(data = data, prevKey = null, nextKey = data.lastOrNull()?.jobId)
}
}

最佳答案

我遇到了同样的错误,这对我有用。在 JodSliderDataSource 类(class), toLoadResult 方法,设置 nextKey 通过从响应数据中获取页码并加一来获取参数值。

    private fun toLoadResult(
data: ArrayList<JobDetail>
): LoadResult<Int, JobDetail> {
return LoadResult.Page(
data = data,
prevKey = null,
nextKey = data.lastOrNull()?.jobId + 1 // Add one to the page number here.
)
}

关于android-studio - 相同的值作为 nextKey 在从 Paging Library 3 Android 中的 PagingSource 加载的两个连续页面中传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63870064/

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