Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ค้นหาใน Custom ListView ใน Android ด้วย Kotlin

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการเพิ่มฟังก์ชันค้นหา (Search/Filter) ใน ListView แบบกำหนดเองใน Android โดยใช้ ArrayAdapter และ TextWatcher ด้วย Kotlin

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอหลัก (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/etSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search here" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/etSearch" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.kt

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var listView: ListView
    private lateinit var etSearch: EditText
    private val months = mutableListOf()
    private var arrayAdapter: ArrayAdapter<String>? = null

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

        listView = findViewById(R.id.listView)
        etSearch = findViewById(R.id.etSearch)

        // ข้อมูลตัวอย่าง: เดือน 12 เดือน
        months.addAll(listOf(
            "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
        ))

        arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, months)
        listView.adapter = arrayAdapter

        // TextWatcher สำหรับกรองข้อมูลแบบ Real-time
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                arrayAdapter?.filter?.filter(s)
            }
            override fun afterTextChanged(s: Editable) {}
        })
    }
}

ขั้นตอนที่ 4: ตั้งค่า AndroidManifest.xml

ใช้การตั้งค่ามาตรฐาน

ทดสอบการทำงาน

รันแอป พิมพ์ในช่อง Search (เช่น "Jan") ListView จะกรองแสดงเฉพาะรายการที่ตรงกัน

ข้อมูลเพิ่มเติม

  • ArrayAdapter.filter ใช้ Filter class ภายใน ทำงานบน Background Thread อัตโนมัติ
  • Custom Adapter: หากใช้ Custom Adapter ต้อง implement Filterable และ override getFilter()
  • SearchView: ใช้ SearchView ใน Toolbar/ActionBar แทน EditText สำหรับ UI มาตรฐาน
  • RecyclerView: แอปทันสมัยใช้ RecyclerView + Filterable interface ใน Adapter
  • Performance: สำหรับข้อมูลเยอะ (>1000 รายการ) ควรใช้ Room Database + FTS (Full Text Search) หรือ Library เช่น SearchView/Algolia