'Doesn't show retrofit + viewmodel data

Good evening. Faced a problem when using retrofit, viewmodel. The problem is that the application starts but no data is displayed. I hope you'll give me a hand! If you need more information, feel free to write!

API I am using - https://swapi.dev/documentation

My model code:

data class Characters(
    @SerializedName("name") val characters: String,
    @SerializedName("gender") val gender: String,
    @SerializedName("height") val height: String,
    @SerializedName("mass") val mass: String,
)

Viewmodel

class HomeViewModel() : ViewModel() {

    var repository = StarWarsRepository()
    val list: MutableLiveData<Response<Characters>> = MutableLiveData()

    fun getCharacterViewModel() {
        viewModelScope.launch {
            list.value = repository.getCharacters()
        }
    }
}

Fragment

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    private lateinit var viewModel: HomeViewModel
    lateinit var recyclerView: RecyclerView
    private lateinit var adapter: HomeListAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewModel = ViewModelProvider(this).get((HomeViewModel::class.java))
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val view = binding.root

        recyclerView = binding.recycler
        adapter = HomeListAdapter()
        recyclerView.adapter = adapter
        viewModel.getCharacterViewModel()
        viewModel.list.observe(this, { list ->
            list.body()?.let {
                adapter.setList(it)
            }
        })
        return view

    }


}

Repository

class StarWarsRepository() {

    suspend fun getCharacters(): Response<Characters> {
        return RetrofitBuilder.api.getCharacters()
    }
}

Adapter

class HomeListAdapter() :
    RecyclerView.Adapter<HomeListAdapter.HomeViewHolder>() {
    var peopleList = ArrayList<Characters>()

    class HomeViewHolder(var binding: ItemCharacterBinding) :
        RecyclerView.ViewHolder(binding.root) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
        return HomeViewHolder(
            ItemCharacterBinding.inflate(
                LayoutInflater
                    .from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
        val characterList = peopleList[position]
        holder.binding.characterName.text = characterList.characters
    }

    override fun getItemCount(): Int {
        return peopleList.size
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setList(list: Characters) {
        peopleList = arrayListOf(list)
        notifyDataSetChanged()
    }

    }

Interface

interface StarWarsApi {

    @GET("people/")
    suspend fun getCharacters(): Response<Characters>

}


Solution 1:[1]

It appears that you have not used a LayoutManager in your recycler view.

Before adding adapter, or on onCreateView, call:

recyclerView.layoutManager=LinearLayoutManager(requireContext())
recyclerView.adapter = adapter

Also, try to log the list before setting it:

Log.i("HomeFragment", "list log: $it")
adapter.setList(it)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdullah Z Khan