gpt4 book ai didi

android - 为什么我的 PagingSource 没有给我任何数据?

转载 作者:行者123 更新时间:2023-12-04 23:49:54 26 4
gpt4 key购买 nike

项目中不起作用的元素
我检查数据是否不为空,并在 fragment 中执行默认的 submitList。
顺便说一句,这里是文档的链接
搜索分页源
这些日志甚至没有显示

    class SearchPagingSource(
private val api: Api,
private val query: String
) : PagingSource<Int, Image>
() {

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Image> {
val position = params.key ?: 0

Log.d("SUPERTAG", "response")
return try {
val response =
api.search(query, position, params.loadSize, Locale.getDefault().language)
val list = ArrayList<Image>()
response.gif.forEach {
list.add(it.image)
}
Log.d("SUPERTAG", "response: $list")
LoadResult.Page(
data = list,
prevKey = null,
nextKey = if (list.isEmpty()) null else position + 15
)
} catch (e: IOException) {
// no connection
Log.d("SUPERTAG", "IOException: ${e.message}")
LoadResult.Error(e)
} catch (e: HttpException) {
// error loading
Log.d("SUPERTAG", "HttpException: ${e.message}")
LoadResult.Error(e)
}
}
}
查看型号
Null 因为存储库返回的 null。
class SearchViewModel : ViewModel() {
private val searchRepository = SearchRepository.getInstance()

private val _query = MutableLiveData<String>()

private val _results = _query.map { data ->
searchRepository.search(data).value
}

val results = _results

private val _error = MutableLiveData<String>()
val error: LiveData<String> = _error

@SuppressLint("StaticFieldLeak")
private lateinit var progressBar: ProgressBar

fun initProgress(progress: ProgressBar) {
progressBar = progress
}

fun search(query: String, errorLoading: String) {
viewModelScope.launch {
try {
progressBar.visibility = View.VISIBLE
_query.value = query
Log.d("SUPERTAG", "result2: ${searchRepository.search(_query.value!!).value}")
progressBar.visibility = View.GONE
} catch (e: Exception) {
_error.value = e.message
}
}
}
}
存储库
正是这部分代码返回null,我通过日志检查了它。我想我在参数或一般情况下做错了。
object SearchRepository {
private lateinit var instance: SearchRepository
private val app: App by lazy {
App().getInstance()
}

fun getInstance(): SearchRepository {
instance = this
app.initRetrofit()
return instance
}

fun search(query: String) = Pager(
config = PagingConfig(
15,
maxSize = 50,
enablePlaceholders = false
),
pagingSourceFactory = {
SearchPagingSource(app.api, query)
}
).liveData

}
如果我这样做,我至少会收到 snackbar 和错误消息。通常它什么也不显示,甚至没有进度条。
所以,如果我添加 jumpThreshold = 0,我会得到一个带有我通常没有的错误的 snackbar 。
fun search(query: String) = Pager(
config = PagingConfig(
pageSize = 15,
jumpThreshold = 0
),
pagingSourceFactory = {
SearchPagingSource(app.api, query)
}
).liveData
编辑
所以,我用 flow 做了,它有点用,但我仍然没有在我的回收站中得到一个列表。
存储库
fun getListData(query: String): Flow<PagingData<Image>> {
return Pager(
config = PagingConfig(
pageSize = 15,
maxSize = 50,
enablePlaceholders = true
), pagingSourceFactory = {
SearchPagingSource(query = query, api = app.api)
}).flow
}
查看型号
fun search(query: String){
viewModelScope.launch {
try {
searchRepository.getListData(query).collectLatest {
it.map {
Log.d("SUPERTAG", "image $it")
}
Log.d("SUPERTAG", it.toString())
_results.value = it
}
} catch (e: Exception){
_error.value = e.message
}
}
}

最佳答案

答案在适配器中!

    class SearchAdapter(
diffCallback: DiffUtil.ItemCallback<Image>,
private val clickListener: (url: String) -> Unit
) : PagingDataAdapter<Image, SearchAdapterViewHolder>(diffCallback) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchAdapterViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.list_item, parent, false)
return SearchAdapterViewHolder(view)
}

override fun onBindViewHolder(holder: SearchAdapterViewHolder, position: Int) {
val item = getItem(position)

if(item != null){
holder.imageView.setOnClickListener {
clickListener(item.original.url)
}
holder.bind(item)
}
}
}

关于android - 为什么我的 PagingSource 没有给我任何数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67322278/

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