我无法从 API 显示我的数据?Kotlin Android



第一个是我的repo: https://github.com/Berkayszk/FiWoApp

我得到我的数据使用改造和使用分页适配器。当我想在我的MovieFragment中使用我的recyclerView显示我的数据时,我看到没有数据来了。这个错误的原因是什么?这个问题我已经处理了3天了,可还是找不到解决办法。

<<p>电影片段/strong>=放映我的电影
@AndroidEntryPoint
class MovieFragment : Fragment(R.layout.fragment_movie) {
private var _binding: FragmentMovieBinding? = null
private val binding get() = _binding!!
private val viewModel: MovieViewModel by viewModels()
private lateinit var movieAdapter: PopularMovieAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
_binding = FragmentMovieBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

loadingData()
setUpRv()
}

private fun loadingData() {
movieAdapter = PopularMovieAdapter()
lifecycleScope.launch {
viewModel.moviesList.collect { pagingData ->
movieAdapter.submitData(pagingData)
}
}
}
private fun setUpRv(){
movieAdapter = PopularMovieAdapter()
binding.recyclerView.apply {
layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
adapter = movieAdapter
setHasFixedSize(true)
}
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}

}

PopularMovieAdapter=分页适配器

class PopularMovieAdapter :  PagingDataAdapter<com.example.fiwoapp.model.popularmovie.Result,PopularMovieAdapter.PopularMovieHo lder>(
diffCallBack) {
class PopularMovieHolder(val binding : PopularMovieRowBinding) : ViewHolder(binding.root)
companion object {
val diffCallBack = object : DiffUtil.ItemCallback<com.example.fiwoapp.model.popularmovie.Result>(){
override fun areItemsTheSame(oldItem: com.example.fiwoapp.model.popularmovie.Result, newItem: com.example.fiwoapp.model.popularmovie.Result): Boolean {
return oldItem.id==newItem.id
}
override fun areContentsTheSame(oldItem: com.example.fiwoapp.model.popularmovie.Result, newItem: com.example.fiwoapp.model.popularmovie.Result): Boolean {
return oldItem == newItem
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PopularMovieHolder {
return PopularMovieHolder(PopularMovieRowBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}
override fun onBindViewHolder(holder: PopularMovieHolder, position: Int) {
val currentItem = getItem(position)
holder.binding.apply {
movieName.text = currentItem!!.title
val imageLink = "${Constants.IMAGE_BASE_UR}+${currentItem.poster_path}"
imageView.load(imageLink){
crossfade(true)
crossfade(100)
}
}
}
<<p>分页来源/strong>
class PopularMovieSource(private val repository: MovieShowRepository) :PagingSource<Int,Result>(){
override suspend fun load(params: LoadParams<Int>): LoadResult<Int,Result> {
return try {
val currentPage = params.key ?: 1
val response = repository.getPopularMovie(currentPage)
val data = response.body()!!.results
val responseData = mutableListOf<Result>()
responseData.addAll(data)
LoadResult.Page(
data = responseData,
prevKey = if (currentPage == 1) null else -1,
nextKey = currentPage.plus(1)
)
} catch (e: Exception) {
LoadResult.Error(e)
} catch (exception: HttpException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, com.example.fiwoapp.model.popularmovie.Result>): Int? {
return null
}

我ViewModel

@HiltViewModel class MovieViewModel @Inject constructor(
private val repository: MovieShowRepository,
private val apiService: ApiService

): ViewModel() {

val loading = MutableLiveData<Boolean>()
val moviesList = Pager(PagingConfig(1)) {
PopularMovieSource(repository)
}.flow.cachedIn(viewModelScope)
//Api
val detailsMovie = MutableLiveData<DetailsResponse>()
fun loadDetailsMovie(id: Int) = viewModelScope.launch {
loading.postValue(true)
val response = repository.getMovieDetails(id)
if (response.isSuccessful) {
detailsMovie.postValue(response.body())
}
loading.postValue(false)
}

我回购

class MovieShowRepository @Inject constructor(val apiService : ApiService){
suspend fun getPopularMovie(page : Int) = apiService.getPopularMovie()
suspend fun getMovieDetails(id:Int) = apiService.getMovieDetails(id)

你可以在我的代码库中看到我所有的代码。当我的应用程序运行时,我没有得到任何错误,但它没有显示在任何yield。当我检查不同的repos时,我看到应用程序在与我相同的代码行的项目中工作,但它不能在我的项目中工作,非常感谢您的帮助。

错误:

private fun loadingData() {
movieAdapter = PopularMovieAdapter()
lifecycleScope.launch {
viewModel.moviesList.collect { pagingData ->
movieAdapter.submitData(pagingData)
}
}
}
private fun setUpRv() {
movieAdapter = PopularMovieAdapter() // create a new adapter?
binding.recyclerView.apply {
layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
adapter = movieAdapter
setHasFixedSize(true)
}
}

正确的:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpRv() // 1st - setup RV
loadingData() // 2nd - load data
}

private fun loadingData() {
lifecycleScope.launch {
viewModel.moviesList.collect { pagingData ->
movieAdapter.submitData(pagingData)
}
}
}

在您的代码中,loadingData()函数在setUpRv()函数之前调用。因此,当setUpRv()被调用时,movieAdapter被创建为默认值,而不是pagingData的当前数据。

相关内容

  • 没有找到相关文章

最新更新