0

basically i have to get data from an api "https://api.ipma.pt/open-data/distrits-islands.json" and show the cities on a spinner, for that I used moshi to get the data and a spinner adapter. can someone help me fix the error of "Unchecked cast: List <SiteData?>? to List <SiteData.DataClass>"?

this is MainActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val botao: Button=findViewById(R.id.couf)
    botao.setOnClickListener{ changeCF() }

    val moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()

    val retrofit = Retrofit.Builder()
        .baseUrl( AppApiService.BASE_URL)
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .build()

    val API = retrofit.create(AppApiService::class.java)

    val call = API.posts
    call?.enqueue(object : Callback<List<SiteData?>?>{
        override fun onResponse(
            call: Call<List<SiteData?>?>,
            response: Response<List<SiteData?>?>
        ) {
            var postList : List<SiteData.DataClass>?= response.body() as List<SiteData.DataClass>



            var spinner: Spinner= findViewById(R.id.spinner)
            spinner.adapter=SpinnerAdapter(this@MainActivity,postList!!)

        }

        override fun onFailure(call: Call<List<SiteData?>?>, t: Throwable) {
            TODO("Not yet implemented")
        }

    })


}






@Override
private fun changeCF(){
    Toast.makeText(this,"Unidade Convertida",Toast.LENGTH_SHORT).show()
    val txt: TextView = findViewById(R.id.temp)
    val texto = txt.text.toString()
    if(texto == "13.5ºC") {
        txt.setText("56.3ºF")
    } else
        txt.setText("13.5ºC")
    }}

this is my Data class

class SiteData (var owner: String,
            var country:String,
            var data: List<DataClass>
            ){
data class DataClass(
    var local: String,
    var longitude: Double)}

this is my Spinner adapter

class SpinnerAdapter internal constructor(internal var context: Context, internal var list: List<SiteData.DataClass>) : BaseAdapter() {
override fun getCount(): Int {
    return list.size
}

override fun getItem(position: Int): Any? {
  return null
}

override fun getItemId(position: Int): Long {
   return 0
}

override fun getView(i: Int, view: View?, viewGroup: ViewGroup?): View {
    var view = view
    if (view == null) {
        val inflater = LayoutInflater.from(context)

        view = inflater.inflate(R.layout.item, viewGroup, false)
    }

    val textView = view!!.findViewById<TextView>(R.id.textView)

    textView.text =  list[i].local

    return textView
}}

this is my api service

interface AppApiService {
companion object {
    const val BASE_URL = "https://api.ipma.pt/"}

@get:GET("open-data/distrits-islands")

val posts : Call<List<SiteData?>?>?}
  • SiteData and DataClass are two distinct classes that share no hierarchy. You cannot successfully cast one to the other. – Tenfour04 Mar 15 '21 at 20:43
  • Does this answer your question? [How do I address unchecked cast warnings?](https://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings) – Henry Twist Mar 15 '21 at 21:21

0 Answers0