2017-07-31 52 views
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 
    } 
} 

我認爲佈局文件沒有問題。 它在編譯過程中不顯示警告/錯誤。 在設備上運行時會崩潰。運行時錯誤在Android中使用ListView與Kotlin?

logcat的是這裏

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) 

...

+0

請添加您的logcat請 –

+0

您的listView是什麼?目前在您的代碼中沒有看到任何引用或對象? –

+0

@kapsym他正在使用Kotlin –

回答

1

只需修改getView方法簽名如下圖所示:

cv: View? 

控樂趣getView(POS:詮釋,CV:查看? ,pa:ViewGroup):查看

在JAVA converView對象是Nullable。當第一次創建列表項時它將爲空。

+1

非常感謝。我用你的答案完成了事情。但是我怎麼知道一個對象在Java中是否是'Nullable'?在Kotlin說它是無效安全的時候,爲什麼我在編譯期間沒有收到警告或錯誤? – Lucky

+0

@Lucky由於Java不是無效的,Android的ListView類是在Java中定義的。您可以在Java中指定'@ NotNull'或'@ Nullable'(https://kotlinlang.org/docs/reference/java-interop.html),並且Kotlin識別註釋。但是,註釋並不是強制性的,ListView.getView()的定義沒有指定它們。如果沒有指定它們,你可能需要閱讀JavaDoc或源代碼來找出哪些是可以爲空的,哪些是不可空的。 (或者簡單地假定所有未指定的內容都是可空的,並將'?'添加到類型中。) – Naetmul

0

下載源代碼從這裏(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 
} 


} 

謝謝!