ListAdapter does not fill the adapter on 2nd fragment of application

36 Views Asked by At

I have an application with 2 fragments, both have lists that are being filled from the same adapter.

The first one works correctly, but the second one -

class CountryBordersFragment : Fragment(R.layout.fragment_country_borders) {

    private lateinit var selectedCountry: String
    private lateinit var countriesViewModel: CountriesListViewModel
    private lateinit var countriesAdapter: CountriesListAdapter
    private var countriesList = mutableListOf<CountryEntity>()


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        countriesViewModel = ViewModelProvider(this).get(CountriesListViewModel::class.java)
        initData()
        initClickListener()
    }

    private fun initClickListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }
    }

    private fun initData() {
        countriesAdapter = CountriesListAdapter(null)
        countriesAdapter.submitList(countriesList)
        countriesRecyclerView.setHasFixedSize(true)
        countriesRecyclerView.layoutManager = LinearLayoutManager(context)
        countriesRecyclerView.adapter = countriesAdapter

        arguments?.let {
            selectedCountry = it.getString(getString(R.string.countries_list_fragment_selected_country))!!
        }
        countryName.text = selectedCountry
        countriesViewModel.getCountryBorders(selectedCountry).observeOnce(requireActivity(), Observer { countryBorder ->
            if (countryBorder.neighborCountries.isEmpty()) {
                bordersWith.text = getString(R.string.country_border_fragment_country_does_not_have_borders)
                return@Observer
            }
            countriesViewModel.getCountryByCioc(countryBorder.neighborCountries).observe(requireActivity(), Observer { countryEntityList ->
                countriesAdapter.submitList(countryEntityList)
            })
        })
    }
}

Does not fill the adapter at all. It just does not display any list whatsoever.

For sure something is missing because I am able to fill my adapter correctly at the first fragment but coming to this one the list does not pop up.

Here is my ListAdapter implementation -

class CountriesListAdapter(private val callback: CountryViewHolder.OnCountryClickListener?)
    : ListAdapter<CountryEntity, CountryViewHolder>(CountriesDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CountryViewHolder {
        val view = LayoutInflater.from(App.context!!).inflate(R.layout.country_viewholder, parent, false)
        return CountryViewHolder(view)
    }

    override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
        holder.bind(getItem(position), callback)
    }

    class CountriesDiffCallback : DiffUtil.ItemCallback<CountryEntity>() {
        override fun areItemsTheSame(oldItem: CountryEntity, newItem: CountryEntity): Boolean {
            return oldItem.countryName == newItem.countryName
        }

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

    }
}

and my ViewHolder and model -

class CountryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    private val rootLayout: LinearLayout = view.country_viewholder_root_layout
    private val nativeName: TextView = view.country_viewholder_native_name
    private val countryName: TextView = view.country_viewholder_country_name
    private val area: TextView = view.country_viewholder_area
    private val countryImage: ImageView = view.country_viewholder_country_image

    fun bind(model: CountryEntity, callback: OnCountryClickListener?) {
        nativeName.text = App.context!!.getString(R.string.country_view_holder_native_name).plus(" ${model.countryName}")
        countryName.text = App.context!!.getString(R.string.country_view_holder_country_name).plus(" ${model.nativeName}")
        area.text = App.context!!.getString(R.string.country_view_holder_country_area).plus(" ${model.area}")
//        Glide.with(App.context!!).load("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png").into(countryImage)
        Picasso.get().load(model.imageUri).into(countryImage)
        rootLayout.setOnClickListener {
            callback?.onCountryClicked(model.countryName)
        }

    }

    interface OnCountryClickListener {
        fun onCountryClicked(countryName: String)
    }
}
@Entity(tableName = countriesTable, primaryKeys = ["countryName"])
class CountryEntity(
    val countryName: String,
    val nativeName: String,
    val area: Double,
    val cioc: String? = null,
    val imageUri : String? = null
) {

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as CountryEntity

        if (countryName != other.countryName) return false
        if (nativeName != other.nativeName) return false
        if (area != other.area) return false
        if (cioc != other.cioc) return false
        if (imageUri != other.imageUri) return false

        return true
    }

    override fun hashCode(): Int {
        var result = countryName.hashCode()
        result = 31 * result + nativeName.hashCode()
        result = 31 * result + area.hashCode()
        result = 31 * result + (cioc?.hashCode() ?: 0)
        result = 31 * result + (imageUri?.hashCode() ?: 0)
        return result
    }
}

class CountryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    private val rootLayout: LinearLayout = view.country_viewholder_root_layout
    private val nativeName: TextView = view.country_viewholder_native_name
    private val countryName: TextView = view.country_viewholder_country_name
    private val area: TextView = view.country_viewholder_area
    private val countryImage: ImageView = view.country_viewholder_country_image

    fun bind(model: CountryEntity, callback: OnCountryClickListener?) {
        nativeName.text = App.context!!.getString(R.string.country_view_holder_native_name).plus(" ${model.countryName}")
        countryName.text = App.context!!.getString(R.string.country_view_holder_country_name).plus(" ${model.nativeName}")
        area.text = App.context!!.getString(R.string.country_view_holder_country_area).plus(" ${model.area}")
//        Glide.with(App.context!!).load("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png").into(countryImage)
        Picasso.get().load(model.imageUri).into(countryImage)
        rootLayout.setOnClickListener {
            callback?.onCountryClicked(model.countryName)
        }

    }

    interface OnCountryClickListener {
        fun onCountryClicked(countryName: String)
    }
}

What is it that I am missing? Just started working with ListAdapter from normal RecyclerView.Adapter.

1

There are 1 best solutions below

1
On

Looks like you are missing - notifyDataSetChanged()

Just After -

countriesAdapter.submitList(countryEntityList)

Add -

countriesAdapter.notifyDataSetChanged()