How can we implement base adapter with kotlin in android? [closed]
Asked Answered
S

1

5

I'm new in kotlin programming language,how to implement a list view in kotlin using base adapter in android. any one can help me.

Shirleeshirleen answered 11/9, 2017 at 8:53 Comment(5)
it's very similar to android only..Pathogenesis
Write the code in java and convert it into Kotlin using the tools provided by the Android StudioWolford
Can you help me with code.Shirleeshirleen
ok @SamuelRobertShirleeshirleen
What problem have you encountered when you try to write it in Kotlin?Mariehamn
J
26

check this code

public class ListViewActivity() : AppCompatActivity() {

override fun onCreate(savedState: Bundle?) {
    super.onCreate(savedState)
    setContentView(R.layout.activity_list_view)

    val lv = findViewById(R.id.list) as ListView
    lv.adapter = ListExampleAdapter(this)
}

private class ListExampleAdapter(context: Context) : BaseAdapter() {
    internal var sList = arrayOf("Sunday", "Monday")
    private val mInflator: LayoutInflater

    init {
        this.mInflator = LayoutInflater.from(context)
    }

    override fun getCount(): Int {
        return sList.size
    }

    override fun getItem(position: Int): Any {
        return sList[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.list_row, parent, false)
            vh = ListRowHolder(view)
            view.tag = vh
        } else {
            view = convertView
            vh = view.tag as ListRowHolder
        }

        vh.label.text = sList[position]
        return view
    }
}

private class ListRowHolder(row: View?) {
    public val label: TextView

    init {
        this.label = row?.findViewById(R.id.label) as TextView
    }
}
Jamal answered 11/9, 2017 at 9:5 Comment(2)
Thanks @Sk -Sandip KalolaShirleeshirleen
need to implement ThemedSpinnerAdapter alsoProfusion

© 2022 - 2024 — McMap. All rights reserved.