কম্পিউটার টিউটোরিয়াল

Kotlin-এ Android ListView-এ ক্লিক ইভেন্ট হ্যান্ডল করার পদ্ধতি

পূর্ব প্রস্তুতি

এই উদাহরণটি দেখায় কিভাবে ListView-এ আইটেম ক্লিক করলে সিলেক্ট করা আইটেমের নাম TextView-এ দেখানো যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: 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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="Choose is the best football player?"
        android:textColor="@android:color/holo_purple"
        android:textStyle="italic|bold" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView" />

</RelativeLayout>

ধাপ ৩: MainActivity.kt-এ ক্লিক লিসেনার

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.collections.ArrayList

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

        val listView: ListView = findViewById(R.id.listView)
        val textView: TextView = findViewById(R.id.textView)

        val players = arrayOf("Cristiano Ronaldo", "Messi", "Pele", "Ronaldinho", "Maradona", "Neymar")
        val playerList = ArrayList(listOf(*players))

        val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, playerList)
        listView.adapter = arrayAdapter

        listView.setOnItemClickListener { parent, _, position, _ ->
            val selectedItem = parent.getItemAtPosition(position) as String
            textView.text = "The best football player is $selectedItem"
        }
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.listviewclick">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

পরীক্ষা করুন

অ্যাপ চালান। ফুটবলারের নামের লিস্ট থেকে যেকোনো একটি ক্লিক করলে উপরের টেক্সট আপডেট হয়ে সিলেক্ট করা নাম দেখাবে।

টিপস

আধুনিক Android ডেভেলপমেন্টে `RecyclerView` ব্যবহার করা পরামর্শ দেওয়া হয়, কারণ এটি আরও কার্যকর এবং নমনীয়।

  1. JavaScript-এ ইভেন্ট বایند হ্যান্ডল করার পদ্ধতি

  2. অ্যান্ড্রয়েড ListView-এ ক্লিক ইভেন্ট হ্যান্ডল করার পদ্ধতি

  3. Kotlin ব্যবহার করে Android অ্যাপ ব্যাকগ্রাউন্ডে চলছে কিনা চেক করা

  4. অ্যান্ড্রয়েড এমুলেটরের IP ঠিকানা কোটলিন ব্যবহার করে পাওয়ার উপায়