I made this application in Android Studio using Kotlin language. When I made the application an error occurred.
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
val progressBar = findViewById<ProgressBar>(R.id.pb_home)
val dataSource = NetworkProvider.providesHttpAdapter().create(HomeDataSource::class.java)
dataSource.discoverMovie().enqueue(object : Callback<HomeResponse> {
override fun onResponse(
call: Call<HomeResponse>, response:
Response<HomeResponse>
) {
progressBar.visibility = View.GONE
val results = response.body()?.results
val itemAdapter = findViewById<RecyclerView>(R.id.rv_home)
itemAdapter.addItemDecoration(
DividerItemDecoration(this@HomeActivity,
DividerItemDecoration.VERTICAL))
itemAdapter.adapter = HomeAdapter (results ?: emptyList())
}
override fun onFailure(call: Call<HomeResponse>, t: Throwable) {
Log.e(HomeActivity::class.java.simpleName, "${t.printStackTrace()}")
}
})
}
}
Type Mismatch Error occurs when you try to assign a value between two different variable types.
val recyclerView = findViewById(R.id.rv_home) as RecyclerView?
val adapterOBJ = HomeAdapter(results ?: emptyList())
recyclerView?.layoutManager = LinearLayoutManager(this@HomeActivity, RecyclerView.VERTICAL, false)
recyclerView?.adapter = adapterOBJ
Your HomeAdapter will be
class HomeAdapter (private val alList: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
FYI
results?: emptyList()
holds ArrayList<String>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.