Saturday, September 9, 2017

Listview in Kotlin Android

Video Demo:



As we know that now kotlin is officially supported by Android studio. So here I am creating a demo to show listview in android by using kotlin. Download source code from here.

android kotlin listview example

activity_main.xml:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        android:text="Listview in kotlin"
        android:textSize="20dp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textStyle="bold"/>

    <ListView
        android:layout_width="match_parent"
        android:id="@+id/lv_flower"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

MainActivity.kt:

package com.deepshikha.listviewinkotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

public class MainActivity : AppCompatActivity() {

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

        val al_flower=ArrayList<Model_flower>()
        al_flower.add(Model_flower("Rosa",R.drawable.rosa,"A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over a hundred species and thousands of cultivars"))

        al_flower.add(Model_flower("Lotus",R.drawable.lotus,"Nelumbo nucifera, also known as Indian lotus, sacred lotus, bean of India, Egyptian bean or simply lotus, is one of two extant species of aquatic plant in the family Nelumbonaceae."))

        al_flower.add(Model_flower("Cherry Blossom",R.drawable.nelumbo,"A cherry blossom (or commonly known in Japan as sakura) is the flower of any of several trees of genus Prunus, particularly the Japanese cherry, Prunus serrulata"))

        al_flower.add(Model_flower("Bird of Paradise",R.drawable.birdofparadise,"The birds-of-paradise are members of the family Paradisaeidae of the order Passeriformes. The majority of species are found in eastern Indonesia, Papua New Guinea, and eastern Australia. The family has 42 species in 15 genera"))

        al_flower.add(Model_flower("Tulips",R.drawable.tulips,"The tulip is a Eurasian and North African genus of herbaceous, perennial, bulbous plants in the lily family, with showy flowers. About 75 wild species are accepted"))

        al_flower.add(Model_flower("Dahlia",R.drawable.dahlia,"Dahlia is a genus of bushy, tuberous, herbaceous perennial plants native to Mexico. A member of the Asteraceae, dicotyledonous plants, related species include the sunflower, daisy, chrysanthemum, and zinnia."))

        al_flower.add(Model_flower("Water Lilies",R.drawable.waterlilies,"Lilium is a genus of herbaceous flowering plants growing from bulbs, all with large prominent flowers. Lilies are a group of flowering plants which are important in culture and literature in much of the world."))

        al_flower.add(Model_flower("Gazania",R.drawable.gazania,"Gazania is a genus of flowering plants in the family Asteraceae, native to Southern Africa. They produce large, daisy-like composite flowerheads in brilliant shades of yellow and orange, over a long period in summer."))

        al_flower.add(Model_flower("Orchid",R.drawable.orchid,"The Orchidaceae are a diverse and widespread family of flowering plants, with blooms that are often colourful and fragrant, commonly known as the orchid family. Along with the Asteraceae, they are one of the two largest families of flowering plants"))


        val obj_adapter : CustomAdapter
        obj_adapter = CustomAdapter(applicationContext,al_flower)
        lv_flower.adapter=obj_adapter
    }

}

adapter_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="#ffffff"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_flower"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:textColor="#000000"
            android:textSize="15dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_des"
            android:maxLines="5"
            android:ellipsize="end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textColor="#000000"
            android:textSize="13dp"/>
    </LinearLayout>


</LinearLayout>

CustomAdapter.kt:

package com.deepshikha.listviewinkotlin

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView

/**
 * Created by deepshikha on 31/7/17.
 */

class CustomAdapter(context: Context,al_flower:ArrayList<Model_flower>) : BaseAdapter(){

    private val mInflator: LayoutInflater
    private val al_flower:ArrayList<Model_flower>

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

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

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

        vh.label.text = al_flower.get(position).str_name
        vh.tv_des.text = al_flower.get(position).str_des
        vh.iv_image.setImageResource(al_flower.get(position).int_image)
        return view
    }
}

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

    init {
        this.label = row?.findViewById<TextView>(R.id.tv_name) as TextView
        this.tv_des = row?.findViewById<TextView>(R.id.tv_des) as TextView
        this.iv_image = row?.findViewById<ImageView>(R.id.iv_flower) as ImageView
    }


}

Model_flower.kt:

package com.deepshikha.listviewinkotlin

/**
 * Created by deepshikha on 31/7/17.
 */
public class Model_flower{
    var str_name:String =""
    var str_des:String =""
    var int_image:Int=0

    constructor(str_name:String,int_image:Int,str_des:String){
        this.int_image=int_image
        this.str_name=str_name
        this.str_des=str_des

    }

}





No comments:

Post a Comment