在Android和Kotlin中使用ListView时出错?

在Android和Kotlin中使用ListView时出错?,android,listview,kotlin,Android,Listview,Kotlin,…只需修改getView方法签名,如下所示: 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.lu

只需修改
getView
方法签名,如下所示:

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)
覆盖视图(位置:Int,cv:View?,pa:ViewGroup):视图


在JAVA
converView
中,对象是可为空的。第一次创建列表项时,它将为空。

只需修改
getView
方法签名,如下所示:

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)
覆盖视图(位置:Int,cv:View?,pa:ViewGroup):视图


在JAVA
converView
中,对象是可为空的。第一次创建列表项时,它将为空。

从此处下载源代码()

MainActivity.kt:

cv: View?
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
}
package com.deepshikha.listviewinkotlin
导入android.os.Bundle
导入android.support.v7.app.AppActivity
导入kotlinx.android.synthetic.main.activity\u main*
公共类MainActivity:AppCompatActivity(){
重写onCreate(savedState:Bundle?){
super.onCreate(savedState)
setContentView(R.layout.activity_main)
val al_flower=ArrayList()
al_flower.add(Model_flower(“Rosa”,R.drawable.Rosa,“玫瑰是蔷薇科蔷薇属的木本多年生开花植物,或其所结的花。有上百种和数千个品种”))
al_flower.add(Model_flower(“Lotus”,R.drawable.Lotus,“Nelumbo nucifera,也称为印度莲花、神圣莲花、印度豆、埃及豆或简单的莲花,是莲科中现存的两种水生植物之一”)
al_flower.add(Model_flower(“樱花”,R.drawable.nelumbo),“樱花(或在日本通常称为樱花)是几种李属树的花,尤其是日本樱花(Prunus serrulata”))
al_flower.add(Model_flower(“天堂鸟”,R.drawable.birdofparadise,“天堂鸟是雀形目天堂鸟科的成员。大多数物种分布在印度尼西亚东部、巴布亚新几内亚和澳大利亚东部。该科有15属42种”))
al_flower.add(Model_flower(“郁金香”,R.drawable.Tulips,“郁金香是欧亚大陆和北非百合科草本、多年生、球茎植物的一个属,花艳丽。可接受约75种野生物种”))
al_flower.add(Model_flower(“Dahlia”,R.drawable.Dahlia,“Dahlia是原产于墨西哥的一个灌木、块茎、草本多年生植物属。菊科双子叶植物的成员,相关物种包括向日葵、雏菊、菊花和百日草。”)
al_flower.add(Model_flower(“睡莲”,R.drawable.Waterliles,“百合是一种草本开花植物,由鳞茎植物生长而成,所有植物都有巨大的突出花朵。百合是一组开花植物,在世界许多地方的文化和文学中都很重要。”)
al_flower.add(Model_flower(“Gazania”,R.drawable.Gazania,“Gazania是菊科开花植物的一个属,原产于南部非洲。在夏季的很长一段时间内,它们产生黄色和橙色明暗相间的大型雏菊状复合花头。”)
al_flower.add(Model_flower(“兰花”,R.drawable.兰花),“兰科是一个种类繁多、分布广泛的开花植物科,其花朵通常五彩缤纷、芳香四溢,俗称兰科。它们与菊科一起,是两个最大的开花植物科之一”)
val obj_适配器:自定义适配器
obj_adapter=CustomAdapter(applicationContext,al_)
lv_flower.适配器=obj_适配器
}
}

CustomAdapter.kt:

cv: View?
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
}
package com.deepshikha.listviewinkotlin
导入android.content.Context
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.BaseAdapter
导入android.widget.ImageView
导入android.widget.TextView
/**
*由deepshikha于2017年7月31日创建。
*/
类CustomAdapter(上下文:context,al_flower:ArrayList):BaseAdapter(){
私人val mInflator:LayoutInflater
私人val al_flower:ArrayList
初始化{
this.mInflator=LayoutInflater.from(上下文)
这个.al_flower=al_flower
}
重写fun getCount():Int{
返回al_花的大小
}
覆盖趣味getItem(位置:Int):任意{
返回al_flower.get(位置)
}
覆盖getItemId(位置:Int):长{
返回位置。toLong()
}
覆盖视图(位置:Int,convertView:View?,父级:ViewGroup):视图{
val视图:视图?
val vh:ListRowHolder
if(convertView==null){
视图=此.mInflator.充气(R.layout.adapter\u布局,父级,false)
vh=列表行保持器(视图)
view.tag=vh
}否则{
视图=转换视图
vh=view.tag作为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)
返回视图
}
}
私有类ListRowHolder(行:视图?){
公共val标签:TextView
公共价值电视:文本视图
公共val iv_图像:ImageView
初始化{
this.label=row?.findViewById(R.id.tv\u名称)作为文本视图
this.tv_des=row?.findViewById(R.id.tv_des)作为文本视图
this.iv_image=row?.findViewById(R.id.iv_flower)作为ImageView
}
}

谢谢

从这里下载源代码()

MainActivity.kt:

cv: View?
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
}
package com.deepshikha.listviewinkotlin
导入android.os.Bundle
导入android.support.v7.app.AppActivity
导入kotlinx.android.synthetic.main.activity\u main*
公共类MainActivity:AppCompatActivity(){
重写onCreate(savedState:Bundle?){
super.onCreate(savedState)
setContentView(R.layout.activity_main)
val al_flower=ArrayList()
“玫瑰是蔷薇科蔷薇属的一种木本多年生开花植物,或它所结的花。有上百种和数千种栽培植物