Kotlin:获取点击项列表视图的值(片段+适配器)

Kotlin : get value of the clicked item list view (Fragment + Adapter)

我正在尝试从 ListView 外部的列表视图中获取点击值。当我点击我的项目时,我想在我的片段中填充一个 textView。

在片段中,我有一个 searchView 和一个 ListView。

这是我的适配器:

class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean) : ArrayAdapter<String>(context, resource, list) {

    private val TAG = ListAdapterCustom::class.java.simpleName
    var nameArret: String = ""

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = super.getView(position, convertView, parent)

        val arret = view.findViewById<TextView>(R.id.arret)
        val receivedBundle = arguments    
        arret.setOnClickListener {
            val fragment = HorairePassage()
            fragment.arguments = receivedBundle
            nameArret = arret.text.toString()
            Log.i(TAG, "${arret.text} clicked")
            getArretCode(arret.text.toString(), fragment) //récupération du code arret + changement de fragment (en cascade)
        }

        if (position % 2 == 1) {
            view.setBackgroundResource(R.color.colorWhite)
        } else {
            view.setBackgroundResource(R.color.grayBackground)
        }



        return view
    }

部分片段:

search.setOnQueryTextListener(object : android.support.v7.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(newText: String): Boolean {
                return false
            }

            override fun onQueryTextChange(newText: String): Boolean {
                if(newText.length > 3){
                    loadingArretSearch.visibility = View.VISIBLE
                    getListeArrets(view, newText.trim(), resultArgument)
                    adapter = ListAdapterHoraire(view.context, R.layout.list_adapter, lignes, resultArgument, false)
                    listSearchView.adapter = adapter
                    adapter.filter.filter(newText.trim())
                    if(newText.trim() != ""){
                        listSearchView.visibility = View.VISIBLE
                    }else{
                        listSearchView.visibility = View.GONE
                    }
                }else{
                    lignes.clear()
                    loadingArretSearch.visibility = View.GONE
                    listSearchView.visibility = View.GONE
                }
                return false

            }

        })

对于这种情况 --> 尝试将回调传递到适配器中,然后您可以从 activity 中使用任何方法,然后您可以从 activity 中执行您想要的操作.

class MyRecyclerViewAdapter(params , val itemClick: (Int) -> Unit): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        ...
        itemView.setOnClickListener( {itemClick(layoutPosition)} )
    }
}

In your fragment or Activity 

val myAdapter = MyRecyclerViewAdapter(params) { position ->
    // do something
} 

--> 在你的情况下

ListAdapterHoraire(view.context, R.layout.list_adapter, lignes, resultArgument, false){value->
  // do something
}  

class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean,onItemClicked: (text: String) -> Unit) : ArrayAdapter<String>(context, resource, list) {

         ...


         arret.setOnClickListener { onItemClicked(arret.text.toString()) }

         ...

        }

尝试在构造函数中传递一个函数作为参数,并在调用 onClickListener 时调用它:

class ListAdapterHoraire (context: Context, resource: Int, list: ArrayList<String>, private val arguments: Bundle?, private val forScol : Boolean,
 private val onItemClicked: (text: String) -> Unit) : ArrayAdapter<String>(context, resource, list) {       
         ...


         arret.setOnClickListener { onItemClicked(arret.text.toString()) }

         ...

        }

因此,在您的片段中更新适配器实例

adapter = ListAdapterHoraire(view.context, R.layout.list_adapter, 
             lignes, resultArgument, false,
             { text -> Log.i("TAG", text) /** Do something here **/ })
 ...