How to add a custom adapter to an AutoCompleteTextView

前端 未结 6 1111
感情败类
感情败类 2021-02-14 08:35

Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.

There is android.R.layout.two_line_list_item Which I couldn\'t find any exa

6条回答
  •  情歌与酒
    2021-02-14 09:27

    Here's an extension for AutoCompleteTextView, Kotlin

    fun AutoCompleteTextView.showListDropDown(list: List, action:(item: Any) -> Unit){
    
        val adapter = ArrayAdapter(
            this.context,
            R.layout.custom_dropdown_item,
            ArrayList(list)
        )
    
        this.setAdapter(adapter)
    
        this.threshold = 1
    
        this.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val item = adapter.getItem(position)!!
            action(item)
        }
    
        this.setOnTouchListener { _: View?, _: MotionEvent? ->
            if (list.isNotEmpty()) {
                if (this.text.toString() != "") adapter.filter
                    .filter(null)
                this.showDropDown()
            }
            return@setOnTouchListener true
        }
    }
    

提交回复
热议问题