Reputation: 241
class MainActivity : AppCompatActivity() {
var data = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mAdapter = MAdapter(this,R.layout.mitem, data)
setContentView(R.layout.activity_main)
listView.adapter = mAdapter
}
}
class MAdapter(context: Context, var id: Int, objects: List<String>) :
ArrayAdapter<String>(context, id, objects) {
override fun getView(pos: Int, cv: View, pa: ViewGroup): View {
val mItem = getItem(pos)
val v = LayoutInflater.from(context).inflate(id, pa, false)
v.itemid.text = mItem
v.itemdes.text = "common description"
return v
}
}
I think the layout file has no problem. It shows no warnings/errors in compiling process. It crashes when running on devices.
logcat is here
07-31 17:58:38.470 30321-30321/? E/dalvikvm: >>>>> Normal User
07-31 17:58:38.470 30321-30321/? E/dalvikvm: >>>>> com.lukhy.landroid [ userId:0 | appId:10211 ]
07-31 17:58:50.950 30321-30321/com.lukhy.landroid E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
07-31 17:58:51.110 30321-30321/com.lukhy.landroid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lukhy.landroid, PID: 30321
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter cv
at com.lukhy.landroid.MAdapter.getView(MainActivity.kt)
at android.widget.AbsListView.obtainView(AbsListView.java:2753)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
at android.widget.ListView.onMeasure(ListView.java:1186)
at android.view.View.measure(View.java:17442)
...
Upvotes: 0
Views: 938
Reputation: 2094
Download source code from here (Listview in Kotlin Android)
MainActivity.kt:
package com.deepshikha.listviewinkotlin
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
public class MainActivity : AppCompatActivity() {
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
setContentView(R.layout.activity_main)
val al_flower=ArrayList<Model_flower>()
al_flower.add(Model_flower("Rosa",R.drawable.rosa,"A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over a hundred species and thousands of cultivars"))
al_flower.add(Model_flower("Lotus",R.drawable.lotus,"Nelumbo nucifera, also known as Indian lotus, sacred lotus, bean of India, Egyptian bean or simply lotus, is one of two extant species of aquatic plant in the family Nelumbonaceae."))
al_flower.add(Model_flower("Cherry Blossom",R.drawable.nelumbo,"A cherry blossom (or commonly known in Japan as sakura) is the flower of any of several trees of genus Prunus, particularly the Japanese cherry, Prunus serrulata"))
al_flower.add(Model_flower("Bird of Paradise",R.drawable.birdofparadise,"The birds-of-paradise are members of the family Paradisaeidae of the order Passeriformes. The majority of species are found in eastern Indonesia, Papua New Guinea, and eastern Australia. The family has 42 species in 15 genera"))
al_flower.add(Model_flower("Tulips",R.drawable.tulips,"The tulip is a Eurasian and North African genus of herbaceous, perennial, bulbous plants in the lily family, with showy flowers. About 75 wild species are accepted"))
al_flower.add(Model_flower("Dahlia",R.drawable.dahlia,"Dahlia is a genus of bushy, tuberous, herbaceous perennial plants native to Mexico. A member of the Asteraceae, dicotyledonous plants, related species include the sunflower, daisy, chrysanthemum, and zinnia."))
al_flower.add(Model_flower("Water Lilies",R.drawable.waterlilies,"Lilium is a genus of herbaceous flowering plants growing from bulbs, all with large prominent flowers. Lilies are a group of flowering plants which are important in culture and literature in much of the world."))
al_flower.add(Model_flower("Gazania",R.drawable.gazania,"Gazania is a genus of flowering plants in the family Asteraceae, native to Southern Africa. They produce large, daisy-like composite flowerheads in brilliant shades of yellow and orange, over a long period in summer."))
al_flower.add(Model_flower("Orchid",R.drawable.orchid,"The Orchidaceae are a diverse and widespread family of flowering plants, with blooms that are often colourful and fragrant, commonly known as the orchid family. Along with the Asteraceae, they are one of the two largest families of flowering plants"))
val obj_adapter : CustomAdapter
obj_adapter = CustomAdapter(applicationContext,al_flower)
lv_flower.adapter=obj_adapter
}
}
CustomAdapter.kt:
package com.deepshikha.listviewinkotlin
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
/**
* Created by deepshikha on 31/7/17.
*/
class CustomAdapter(context: Context,al_flower:ArrayList<Model_flower>) : BaseAdapter(){
private val mInflator: LayoutInflater
private val al_flower:ArrayList<Model_flower>
init {
this.mInflator = LayoutInflater.from(context)
this.al_flower=al_flower
}
override fun getCount(): Int {
return al_flower.size
}
override fun getItem(position: Int): Any {
return al_flower.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
val view: View?
val vh: ListRowHolder
if (convertView == null) {
view = this.mInflator.inflate(R.layout.adapter_layout, parent, false)
vh = ListRowHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ListRowHolder
}
vh.label.text = al_flower.get(position).str_name
vh.tv_des.text = al_flower.get(position).str_des
vh.iv_image.setImageResource(al_flower.get(position).int_image)
return view
}
}
private class ListRowHolder(row: View?) {
public val label: TextView
public val tv_des: TextView
public val iv_image: ImageView
init {
this.label = row?.findViewById<TextView>(R.id.tv_name) as TextView
this.tv_des = row?.findViewById<TextView>(R.id.tv_des) as TextView
this.iv_image = row?.findViewById<ImageView>(R.id.iv_flower) as ImageView
}
}
Thanks!
Upvotes: 0
Reputation: 2613
Just modify getView
method signature like below:
cv: View?
override fun getView(pos: Int, cv: View?, pa: ViewGroup): View
In JAVA converView
object is Nullable
. It will be null when the list item created for the first time.
Upvotes: 2