'getView'不会覆盖任何内容

时间:2019-07-25 12:16:52

标签: android-studio kotlin overriding getview nothing

我是关于android studio的新手。我尝试使用ArrayAdapter。但是,当我完成代码后,它会显示“'getView'不会覆盖任何内容”。我在“返回teksatirview代码中得到此错误”。 这是我的arrayadapter类:

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
}

}

3 个答案:

答案 0 :(得分:4)

您在$str = preg_replace("/&#?[a-z0-9]{2,8};/i","", $str); 中遇到的问题。根据签名,它必须为非null类型。但是它在您的签名中可以为空,因此它在kotlin中的功能完全不同。只需更改为:

parent: ViewGroup?

然后也将类型 override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { 返回到View?

答案 1 :(得分:0)

基本上,您的代码应如下所示:

// 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
}

答案 2 :(得分:0)

帖子可能是正确的,但在这种情况下,代码将无法以优化的方式工作。它为在充气页面中旋转的每个视图对象重新生成。如果数据源很大,这将导致输出效率低下。如果在日志记录中添加并测试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!!
}