Pages

Thursday, 21 December 2017

ListView with Custom BaseAdapter in Kotlin - Android Studio 3.0.1


>> Sample to demonstrate the use of Kotlin in Android

package com.db.listviewbaseadapterkotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ListView
class MainActivity : AppCompatActivity() {
var lv : ListView? = null;
var emojis = arrayOf(
"Smile :) ",
"Wink ;) ",
"Laugh :D ",
"Sad :( ",
"Tongue :p ",
"Angry :@ ",
"Confused :-s ",
"Crying :'( ",
"Evil >D ",
"Surprised :o ",
"Speechless :-|",
"Kiss :* ",
"Devil (6) ",
"Angel (a) ",
"Tired (:| ",
"Sick :-& ",
"Love <3 ",
"Heartbroken (u) ",
"Dont Tell :-# ",
"Sweat #:-S ",
"Blushing :$ ",
"Flower (f) ",
"Sleeping I-) ",
"Afraid :-SS ",
"Jawdrop 8[] "
)
var emojxImg = intArrayOf(
R.drawable.emoticon_smile,
R.drawable.emoticon_wink,
R.drawable.emoticon_laugh,
R.drawable.emoticon_sad,
R.drawable.emoticon_tongue,
R.drawable.emoticon_angry,
R.drawable.emoticon_confused,
R.drawable.emoticon_crying,
R.drawable.emoticon_evil,
R.drawable.emoticon_surprised,
R.drawable.emoticon_speechless,
R.drawable.emoticon_kiss,
R.drawable.emoticon_devil,
R.drawable.emoticon_angel,
R.drawable.emoticon_tired,
R.drawable.emoticon_sick,
R.drawable.emoticon_love,
R.drawable.emoticon_heartbroken,
R.drawable.emoticon_dont_tell,
R.drawable.emoticon_sweat,
R.drawable.emoticon_blushing,
R.drawable.emoticon_flower,
R.drawable.emoticon_sleeping,
R.drawable.emoticon_afraid,
R.drawable.emoticon_jawdrop
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lv = findViewById<View>(R.id.list) as ListView
val emojisAdapter = EmojisAdapter(applicationContext, emojis, emojxImg)
lv!!.setAdapter(emojisAdapter)
}
}
view raw MainActKot.kt hosted with ❤ by GitHub


package com.db.listviewbaseadapterkotlin
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
/**
* Created by DB on 21-12-2017.
*/
class EmojisAdapter(applicationContext: Context?, emojis: Array<String>, emojxImg: IntArray) : BaseAdapter() {
var context: Context? = applicationContext
var emojis: Array<String>? = emojis
var emojxImg: IntArray? = emojxImg
var inflater: LayoutInflater? = null
init {
this.inflater = LayoutInflater.from(context)
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val view : View?
val vh : ListRowHolder
if(convertView == null){
view = this.inflater!!.inflate(R.layout.list_item, viewGroup, false)
vh = ListRowHolder(view)
view?.tag = vh
}else{
view = convertView
vh = view.tag as ListRowHolder
}
vh.emoTxt.setText(emojis!![position])
vh.emoImg.setImageResource(emojxImg!![position])
return view!!
}
override fun getItem(position: Int): Any {
return emojis!![position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return emojis!!.size
}
private class ListRowHolder(row : View?){
val emoTxt : TextView
val emoImg : ImageView
init {
this.emoTxt = row?.findViewById<View>(R.id.emojiName) as TextView
this.emoImg = row?.findViewById<View>(R.id.emojiImg) as ImageView
}
}
}


Github Source: Download Source Here

No comments:

Post a Comment