0
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)

...

4
  • add your logcat please Commented Jul 31, 2017 at 9:56
  • what is your listView? don't see any reference or object for it in your code at the moment? Commented Jul 31, 2017 at 9:57
  • @kapsym he is using Kotlin Commented Jul 31, 2017 at 10:02
  • @OussemaAroua I added the logcat with level "error". Commented Jul 31, 2017 at 10:07

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. I got things done with your answer. But how do I know whether an object is Nullable in Java? And Why didn't I get a warning or error during the compiling time since Kotlin says it's null-safe?
@Lucky Because Java is not null-safe, and ListView class of Android is defined in Java. You can specify @NotNull or @Nullable in Java (kotlinlang.org/docs/reference/java-interop.html), and Kotlin recognizes the annotations. However, the annotations are not mandatory, and ListView.getView()'s definition do not specify them. If they are not specified, you maybe need to read the JavaDoc or source codes to find out which is nullable and which is not-nullable. (Or simply assume everything that are not specified is nullable, and add ? to types.)
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.