Images from Gist not loading in Android Studio using JSON

19 views Asked by At

I've created an app in Android Studio and Generated POJO from JSON using Kotlin language and GSON Framework. Here is the link -> Gist Link.

When I run the app, I am not able to see any of the flag images from the Json link. Does this link require something else in order for the images to show?

I have a recycler view showing the list just fine only the images are not showing. There are no error to report.

Here is my adapter code

class CountryAdapter : RecyclerView.Adapter<CountryAdapter.CountryViewHolder>() {

private var countries = listOf<Country>()


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

override fun onBindViewHolder(holder: CountryViewHolder, position: Int) {
    val country = countries[position]
    holder.bind(country)
}

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

fun setData(countryList: List<Country>) {
    countries = countryList
    notifyDataSetChanged()
}

inner class CountryViewHolder(private val binding: ItemCountryBinding) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(country: Country) {
        binding.nameTextView.text = country.name
        binding.capitalTextView.text = country.capital
        binding.codeTextView.text = country.code
        Glide.with(binding.flagImageView)
            .load(country.flag)
            .into(binding.flagImageView)
    }
}

}

Here is the Main Activity

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var adapter: CountryAdapter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    adapter = CountryAdapter() // Initialize the adapter

    binding.recyclerView.layoutManager = LinearLayoutManager(this)
    binding.recyclerView.adapter = adapter

    val viewModel = ViewModelProvider(this).get(CountryViewModel::class.java)
    viewModel.countryData?.observe(this, Observer { country ->
        country?.let { countryList ->
            adapter.setData(countryList)
        }
    })

    viewModel.fetchCountryData()
}

}

0

There are 0 answers