Home > Software design >  Recyclerview not showing items Kotlin
Recyclerview not showing items Kotlin

Time:06-29

My App is just shows an Empty Screen and RecyclerView is not showing anything or is not working but there are no compile time or run time errors.

It would be great help if I get an answer ... I have been making an app that uses a RecyclerView but it's not showing any thing..why contents of the recycler view have not been showing up.my codes are bellow

Here is My FeedFragment, Adapter

//Fragment

@AndroidEntryPoint
class FeedFragment : Fragment(R.layout.feed_fragment) {

    private var _binding: FeedFragmentBinding? = null
    private val binding get() = _binding!!
    private lateinit var feedAdapter: FeedAdapter
    private val viewModel: FeedViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FeedFragmentBinding.inflate(inflater, container, false)
        val view = binding.root
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupRv()
        getCharacters()
    }

    private fun getCharacters() {
        viewModel.listData.observe(viewLifecycleOwner, Observer { pagingData ->
                feedAdapter.submitData(viewLifecycleOwner.lifecycle, pagingData)
        })
    }


    private fun setupRv() {
        feedAdapter = FeedAdapter()
        binding.feedRv.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        binding.feedRv.apply {
            adapter = feedAdapter
            setHasFixedSize(true)
            visibility = View.VISIBLE
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

//PagingDataAdapter

class FeedAdapter : PagingDataAdapter<Characters,FeedAdapter.FeedViewHolder >(DiffCallback()) {

    inner class FeedViewHolder(private val binding: FeedFragmentRvBinding) :
            RecyclerView.ViewHolder(binding.root) {
                fun bind(characters: Characters) {
                    with(binding) {

                        Glide.with(itemView)
                            .load(characters.image)
                            .transition(DrawableTransitionOptions.withCrossFade())
                            .error(R.drawable.ic_launcher_background)
                            .into(feedRvImage)
                        feedRvName.text = characters.name
                        feedRvStatus.text = characters.status
                        feedRvSpecies.text = characters.species
                        feedRvGender.text = characters.gender

                        println(characters.name)


                    }
                }
            }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
        val binding =
            FeedFragmentRvBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return FeedViewHolder(binding)
    }


    override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {

        getItem(position)?.let { holder.bind(it) }
    }


    class DiffCallback : DiffUtil.ItemCallback<Characters>() {
        override fun areItemsTheSame(oldItem: Characters, newItem: Characters): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Characters, newItem: Characters): Boolean {
            return oldItem == newItem
        }
    }
}

// retrofit response

retrofit response

CodePudding user response:

Update your CharacterResponse model class to parse data accordingly.

data class CharacterResponse(
    val info: Info,
    val results: List<Characters>
)
  • Related