Thursday, October 3, 2019

android kotlin - List filter example

MainActivity.kt

package com.example.jetpack

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        val list = generateList()

        btn.setOnClickListener {
            textView.text = "Employees:\n"
            // Iterate through the list
            list.forEach {
                textView.append("\n${it.name} : age ${it.age} : salary $${it.salary}")
            }
        }

        btnFilter.setOnClickListener {
            val filteredList = list.filter { it.salary>3000 }
            textView.text = "Employees: salary>3000\n"
            // Iterate through the filtered list
            filteredList.forEach {
                textView.append("\n${it.name} : age ${it.age} : salary $${it.salary}")
            }
        }

        btnFilter2.setOnClickListener {
            val filteredList = list.filter { it.salary>=2500 && it.age<=30 }
            textView.text = "Employees: salary>=2500 and age<=30\n"
            // Iterate through the filtered list
            filteredList.forEach {
                textView.append("\n${it.name} : age ${it.age} : salary $${it.salary}")
            }
        }
    }
}

// Method to generate employee list
private fun generateList():MutableList<Employee>{
    val list = mutableListOf<Employee>()
    list.add(Employee("Selina",30,2500))
    list.add(Employee("Madyson",22,2600))
    list.add(Employee("Mable",25,3750))
    list.add(Employee("Elenora",42,4800))
    list.add(Employee("Jovanny",49,5599))
    list.add(Employee("Charlotte",50,4200))
    list.add(Employee("Robbie",36,2875))
    list.add(Employee("Erika",29,1650))
    list.add(Employee("Esta",27,1890))
    return list
}

// Data class for Employee
data class Employee(
    val name:String,
    val age:Int,
    val salary:Int
)
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="All"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/btnFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Filter Sal>3000"
        app:layout_constraintStart_toEndOf="@+id/btn"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/btnFilter2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Filter 2"
        app:layout_constraintStart_toEndOf="@+id/btnFilter"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="List Example"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
More android examples