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

时间:2019-01-11 13:41:15

标签: android android-listview adapter android-arrayadapter

我正在尝试从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

            }

        })

2 个答案:

答案 0 :(得分:1)

对于这样的情况->尝试将回调传递到适配器中,然后您可以在活动中使用任何方法,然后您就可以从活动中执行所需的操作。

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()) }

         ...

        }

答案 1 :(得分:0)

尝试在构造函数中将函数作为参数传递,并在调用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 **/ })
 ...