Still stuck at this Breaking Bad API App. JSON model class is a list but when I'm calling the response in the global scope and putting it the mutable list it says type mismatch: inferred type is List but Collection was expected. How can i fix it
Main Activity
> class MainActivity : AppCompatActivity() {
>
> private lateinit var characterAdapter : MyAdapter
> private var TAG = "MainActivity"
> private var characterData= mutableListOf<ResponseBBItem>()
> override fun onCreate(savedInstanceState: Bundle?) {
> super.onCreate(savedInstanceState)
> setContentView(R.layout.activity_main)
>
> characterAdapter = MyAdapter(characterData , this)
> recyclerView.adapter = characterAdapter
> val layoutMAnager = LinearLayoutManager(this)
> recyclerView.layoutManager = layoutMAnager
> getCharacterInfo()
>
> recyclerView.setOnClickListener {
> getCharacterInfo()
> }
> }
>
> private fun getCharacterInfo() {
> GlobalScope.launch(Dispatchers.IO) {
> try {
> val response = Client.api.getInfo()
> if (response.isSuccessful) {
> val characterList = response.body()
> Log.d(TAG, characterData.toString())
> withContext(Dispatchers.Main) {
> if (characterList!= null) {
> Toast.makeText(this@MainActivity , "Data Loaded"
enter code here
,Toast.LENGTH_SHORT).show()
> characterData.addAll(characterList.responseBB!!)
> characterAdapter.notifyDataSetChanged()
> tvText.text = characterList.toString()
> }
> }
> }
> }
> catch (e:Exception){
> withContext(Dispatchers.Main){
> Toast.makeText(applicationContext, "Cannot Load Data" , Toast.LENGTH_LONG).show()
> }
> }
> }
> } }
I'm getting the error at characterData.addAll(characterList.responseBB!!)
APIRequest
interface APIRequest {
@GET("character/random")
suspend fun getInfo() : Response<ResponseBB>
}
ResponseBB
data class ResponseBB(
@field:SerializedName("ResponseBB")
val responseBB: List<ResponseBBItem?>? = null
)
I believe that 2 ?s in List<ResponseBBItem?>?are giving me error
ResponseBBItem
data class ResponseBBItem(
@field:SerializedName("birthday")
val birthday: Any? = null,
@field:SerializedName("img")
val img: String? = null,
@field:SerializedName("better_call_saul_appearance")
val betterCallSaulAppearance: Any? = null,
@field:SerializedName("occupation")
val occupation: List<String?>? = null,
@field:SerializedName("appearance")
val appearance: List<Int?>? = null,
@field:SerializedName("portrayed")
val portrayed: String? = null,
@field:SerializedName("name")
val name: String? = null,
@field:SerializedName("nickname")
val nickname: String? = null,
@field:SerializedName("char_id")
val charId: Int? = null,
@field:SerializedName("category")
val category: String? = null,
@field:SerializedName("status")
val status: String? = null
)
Client
object Client {
val gson = GsonBuilder().create()
val retrofit = Retrofit.Builder()
.baseUrl("https://www.breakingbadapi.com/api/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
val api = retrofit.create(APIRequest::class.java)
}
MyAdapter
class MyAdapter(var listChar : List<ResponseBBItem?>, val context : Context) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
var name = itemView.findViewById<TextView>(R.id.tvName)
var occupation = itemView.findViewById<TextView>(R.id.tvOccupation)
var protrayed = itemView.findViewById<TextView>(R.id.tvActor)
var status = itemView.findViewById<TextView>(R.id.tvStatus)
var appearance = itemView.findViewById<TextView>(R.id.tvAppearance)
var image = itemView.findViewById<ImageView>(R.id.ivImage)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent , false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.appearance.text = listChar[position]!!.appearance.toString()
holder.name.text = listChar[position]!!.name
holder.occupation.text = listChar[position]!!.occupation.toString()
holder.protrayed.text = listChar[position]!!.portrayed
holder.status.text = listChar[position]!!.status
Picasso.get().load(listChar[position]!!.img).into(holder.image)
}
override fun getItemCount() = listChar.size
}