如何从Kotlin的ListView中获取所选项目?

代码示例:

package tech.kapoor.listviewdemo import android.content.Context import android.graphics.Color import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.ListView import android.widget.TextView import android.widget.AdapterView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val listView = findViewById<ListView>(R.id.main_listview) var redColor = Color.parseColor("#FF0000") listView.setBackgroundColor(redColor) listView.adapter = CustomAdapter(this) } private class CustomAdapter(context: Context): BaseAdapter() { private val mContext: Context init { mContext = context } override fun getCount(): Int { return 80 } override fun getItemId(position: Int): Long { return position.toLong() } override fun getItem(position: Int): Any { return position } override fun getView(position: Int, view: View?, viewGroup: ViewGroup?): View { val textView = TextView(mContext) textView.text = "Here comes the !!" return textView } } } 

我试图理解列表视图而不是回收视图来首先理解基础知识。 任何人都知道我们如何得到所选的行ID /索引值选择或onclick,以及如何执行一些行动选择一个特定的行在KOTLIN?

要填充列表视图,你必须有数据集。 数据集可以是任何数据类型(如字符串)的任何列表,也可以使用模型类的列表。 像这样的东西:

这是我将在ListView中使用的简单数据集列表:

 val data = ArrayList<TopicDTO>() data.add(TopicDTO("1", "Info 1", true)) data.add(TopicDTO("2", "Info 2", false)) data.add(TopicDTO("3", "Info 3", true)) data.add(TopicDTO("4", "Info 4", false)) 

我创建了一个名为TopicDTO的模型类,其中包含id,标题及其状态。

现在让我们将其填充到ListView中:

  list.adapter = ButtonListAdapter(baseContext, data) 

这里是一个简单的适配器:

 class ButtonListAdapter(//Class for rendering each ListItem private val context: Context, private val rowItems: List<TopicDTO>) : BaseAdapter() { override fun getCount(): Int { return rowItems.size } override fun getItem(position: Int): Any { return rowItems[position] } override fun getItemId(position: Int): Long { return rowItems.indexOf(getItem(position)).toLong() } private inner class ViewHolder { internal var main_text: TextView? = null //Display Name internal var subtitle: TextView? = null //Display Description internal var can_view_you_online: Button? = null //Button to set and display status of CanViewYouOnline flag of the class } override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { var convertView = convertView var holder: ViewHolder? = null val mInflater = context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater holder = ViewHolder() if (convertView == null) { convertView = mInflater.inflate(R.layout.main_lp_view_item, null) holder.main_text = convertView!!.findViewById(R.id.main_lp_text) as TextView holder.subtitle = convertView.findViewById(R.id.main_lp_subtitle) as TextView holder.can_view_you_online = convertView.findViewById(R.id.can_view_you_online) as Button convertView.tag = holder } else { holder = convertView.tag as ViewHolder } val rowItem = rowItems[position] val main_text: String val subtitle: String holder.main_text!!.text = rowItem.info holder.subtitle!!.text = rowItem.info if (rowItem.canViewYouOnline) { holder.can_view_you_online!!.setBackgroundColor(context.resources.getColor(R.color.colorPrimary)) } else { holder.can_view_you_online!!.setBackgroundColor(context.resources.getColor(R.color.colorAccent)) } holder.can_view_you_online!!.setOnClickListener(object : View.OnClickListener { internal var buttonClickFlag: Boolean = false override fun onClick(v: View) { //The Onclick function allows one to click the button on the list item and set/reset the canViewYouOnline flag. It is working fine. } }) return convertView } } 

现在你可以得到你选择的项目,如下所示:

 list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> // This is your listview's selected item val item = parent.getItemAtPosition(position) as TopicDTO } 

希望你明白这一点。

您可以在getView()方法内使用如下所示的内容:

 view.setOnClickListener(object : View.OnClickListener { override fun onClick(v: View?) { //use getItem(position) to get the item } }) 

或者使用lambda:

 view.setOnClickListener({ v -> //use theItem(position) }) 

只是一个提示:

我试图理解列表视图而不是回收视图来首先理解基础知识。

在我看来,在您的项目中,您将在99%的案例中使用RecyclerView