'getView' 不覆盖任何内容

'getView' overrides nothing

我是 android 工作室的新手。我尝试使用 ArrayAdapter。但是当我完成我的代码时,它说“'getView' 什么都不覆盖”。我在“return teksatirview 代码中收到此错误。 这是我的数组适配器 class:

package com.example.burcrehberi
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.teksatir.view.*


class BurcArrayAdapter(
var gelencontext: Context,
resource: Int,
textViewResourceId: Int,
var burcAdlari: Array<String>,
var burcTarih: Array<String>,
var burcResimleri: Array<Int>
) : ArrayAdapter<String>(gelencontext, resource, textViewResourceId, burcAdlari) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {

    var teksatirview = convertView

    if (teksatirview == null) {

        var inflater = LayoutInflater.from(gelencontext)
        teksatirview = inflater.inflate(R.layout.teksatir, parent, false)


    }

    var burcImageView = teksatirview?.imgburcsembol
    var burcisim = teksatirview?.tvburcadi
    var burctarih = teksatirview?.tvburctarih

    burcImageView?.setImageResource(burcResimleri[position])
    burcisim?.setText(burcAdlari[position])
    burctarih?.setText(burcTarih[position])

    return teksatirview
}

}

您的问题 parent: ViewGroup?。根据签名,它必须是非空类型。但是它在你的签名中是可以为空的,所以它在 kotlin 中是完全不同的功能。只需更改为:

 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

并且 return 也输入 View?View

基本上你的代码应该是这样的:

// first check your types for function you're overriding
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    // then rewrite internal code to prevent unnecessary optionality
    val teksatirview = convertView ?: {
        var inflater = LayoutInflater.from(gelencontext)
        inflater.inflate(R.layout.teksatir, parent, false)!!
    }()

    val burcImageView = teksatirview.imgburcsembol
    val burcisim = teksatirview.tvburcadi
    val burctarih = teksatirview.tvburctarih

    burcImageView.setImageResource(burcResimleri[position])
    burcisim.setText(burcAdlari[position])
    burctarih.setText(burcTarih[position])

    return teksatirview
}

帖子可能是正确的,但在这种情况下,代码将无法以优化的方式运行。它为 inflater 页面中旋转的每个视图对象重新生成。如果数据源很大,这将导致输出效率低下。如果在日志记录中添加并测试signNames [positions],我想说的会更清楚。作为更优化的解决方案,我可以推荐:

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    var teksatirview = convertView

    if (teksatirview == null) {

        val inflater = LayoutInflater.from(gelencontext)
        teksatirview = inflater.inflate(R.layout.teksatir, parent, false)
        Log.e("TEST","inflation applied ${burcAdlari[position]}")

    }

    var burcImageView = teksatirview?.imgburcsembol
    var burcisim = teksatirview?.tvburcadi
    var burctarih = teksatirview?.tvburctarih

    burcImageView?.setImageResource(burcResimleri[position])
    burcisim?.setText(burcAdlari[position])
    burctarih?.setText(burcTarih[position])

    return teksatirview!!
}