如何使用 Kotlin 获取 listView 中所有选中的项目?
此示例演示了如何使用 Kotlin 获取 listView 中所有选中的项目。
步骤 1 &minusl 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤 2 &minusl 将以下代码添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context="MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <Button android:id="@+id/select" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:text="Select all" /> <Button android:id="@+id/deSelect" android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:text="Deselect all" /> <Button android:id="@+id/viewSelected" android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:text="Next activity" android:visibility="visible" /> </LinearLayout> </LinearLayout>
步骤 3 &minusl 创建一个 Java 类(CustomAdapter.kt)并添加以下代码
package app.com.myapplication
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
class CustomAdapter(private val context: Context, private var modelArrayList: ArrayList<Model>) :
BaseAdapter() {
override fun getViewTypeCount(): Int {
return count
}
override fun getItemViewType(position: Int): Int {
return position
}
override fun getCount(): Int {
return modelArrayList.size
}
override fun getItem(position: Int): Any {
return modelArrayList[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var convertView: View? = convertView
val holder: ViewHolder
if (convertView == null) {
holder = ViewHolder()
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater convertView = inflater.inflate(R.layout.listitem, null, true)
holder.checkBox = convertView!!.findViewById(R.id.checkBox) as CheckBox holder.tvPlayer = convertView.findViewById(R.id.playerNameList) as TextView
convertView.tag = holder
}
else {
// the getTag returns the viewHolder object set as a tag to the view
holder = convertView.tag as ViewHolder
}
holder.checkBox?.text = "Checkbox $position"
holder.tvPlayer!!.text = modelArrayList[position].getPlayer()
holder.checkBox!!.isChecked = modelArrayList[position].getSelected()
holder.checkBox!!.setTag(R.integer.btnPlusPos, convertView)
holder.checkBox!!.tag = position
holder.checkBox!!.setOnClickListener {
val pos = holder.checkBox!!.tag as Int
Toast.makeText(
context, "Checkbox " + pos + "Clicked!",
Toast.LENGTH_SHORT
).show()
if (modelArrayList[pos].getSelected()) {
modelArrayList[pos].setSelected(false)
}
else {
modelArrayList[pos].setSelected(true)
}
}
return convertView
}
private inner class ViewHolder {
var checkBox: CheckBox? = null
var tvPlayer: TextView? = null
}步骤 4 &minusl 创建一个 Java 类(Model.kt)并添加以下代码
package app.com.myapplication
class Model {
private var isSelected = false
private var player: String? = null
fun getPlayer(): String? {
return player
}
fun setPlayer(player: String?) {
this.player = player
}
fun getSelected(): Boolean {
return isSelected
}
fun setSelected(selected: Boolean) {
isSelected = selected
}
}
步骤 5 &minusl 在 res/values/strings.xml 中添加以下代码
<integer name="btnPlusView">1</integer> <integer name="btnPlusPos">2</integer>
步骤 6 &minusl 为你的 listView 创建一个布局 (listItem.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="match_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/playerNameList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:textSize="20sp" /> </LinearLayout>
步骤 7 &minusl 将以下代码添加到 src/MainActivity.kt
package app.com.myapplication
import android.os.Bundle
import android.widget.Button
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var listView: ListView? = null
private var modelArrayList: ArrayList<Model>? = null
private var customAdapter: CustomAdapter? = null
lateinit var btnSelect: Button
lateinit var btnDeSelect: Button
val playerList = arrayOf(
"Sunil Chetri - INDIA",
"Cristiano Ronaldo - Portugal",
"Lionel Messi - Argentina",
"Neymar Jr - Brazil",
"Eden Hazard - Belgium",
"Gigi Buffon - Italy",
"James Rodrigues - Columbia",
"Sadio Mane - Senegal",
"Toni Kroos - Germany"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.listView)
btnSelect = findViewById(R.id.select)
btnDeSelect = findViewById(R.id.deSelect)
modelArrayList = getModel(false)
customAdapter = CustomAdapter(this, modelArrayList!!)
listView!!.adapter = customAdapter
btnSelect.setOnClickListener {
modelArrayList = getModel(true)
customAdapter = CustomAdapter(this@MainActivity, modelArrayList!!)
listView!!.adapter = customAdapter
Toast.makeText(
applicationContext, "Checked all items",
Toast.LENGTH_SHORT
).show()
}
btnDeSelect.setOnClickListener {
modelArrayList = getModel(false)
customAdapter = CustomAdapter(this@MainActivity, modelArrayList!!)
listView!!.adapter = customAdapter
Toast.makeText(
applicationContext, "Unchecked all items",
Toast.LENGTH_SHORT
).show()
}
}
private fun getModel(isSelect: Boolean): java.util.ArrayList<Model>? {
val list: ArrayList<Model> = ArrayList()
for (i in 0..8) {
val model = Model()
model.setSelected(isSelect)
model.setPlayer(playerList[i])
list.add(model)
}
return list
}
}步骤 8 &minusl 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.myapplication"> <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 移动设备连接到您的电脑。要从 Android Studio 运行应用程序,请打开您的项目之一的活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕

点击 这里 下载项目代码。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP