akiilhan
akiilhan

Reputation: 179

'getView' overrides nothing

I am new about android studio. I tried to use ArrayAdapter. But when i complete my codes, it says "'getView' overrides nothing".I getting this error in "return teksatirview code. this is my arrayadapter 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
}

}

Upvotes: 3

Views: 3458

Answers (3)

mrcoder
mrcoder

Reputation: 71

The posts may be correct, but in this case the code will not work in an optimized way. It is regenerated for each view object rotated in the inflater page. This will result in an inefficient output if the data source is large. If the signNames [positions] are added and tested in the log records, what I am trying to say will be clearer. As a more optimized solution, I can recommend:

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

Upvotes: 1

user28434&#39;mstep
user28434&#39;mstep

Reputation: 6600

Basically you code should be like this:

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

Upvotes: 1

Dmitrii Nechepurenko
Dmitrii Nechepurenko

Reputation: 1474

Your problem in parent: ViewGroup?. According to signature it must be non-null type. But it's nullable in your signature, so it's completely different function in kotlin. Just change to:

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

And return type View? to View too.

Upvotes: 11

Related Questions