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

问题描述

项目中不起作用的元素

然后我检查数据是否为空并在片段中执行认提交列表。

顺便说一句,这里是文档的链接

SearchPagingSource

甚至不显示这些日志

    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

}

如果我这样做,我至少会收到小吃店和错误信息。通常它什么都不显示,甚至没有进度条。 所以,如果我添加 jumpThreshold = 0,我会得到一个带有我通常没有的错误的小吃店。

fun search(query: String) = Pager(
            config = PagingConfig(
                pageSize = 15,jumpThreshold = 0
            ),query)
            }
        ).liveData

编辑

所以,我用 flow 做了它,它有点作用,但我仍然没有在我的回收器中得到一个列表。

存储库

fun getListData(query: String): Flow<PagingData<Image>> {
        return Pager(
            config = PagingConfig(
                pageSize = 15,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)
        }
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...