AutoCompleteTextView onItemClickListener无法触发

时间:2019-05-14 04:00:39

标签: android kotlin

我在AlertDialog(自定义视图)中有一个AutoCompleteTextView。我已经注册了onItemClick事件。

当我键入输入内容时,会出现一个下拉选项。但是,当我单击其中一项时,onItemClick事件处理程序将永远不会触发。

我想知道我的代码出了什么问题以及为什么它不会触发事件处理程序。

我尝试通过AutoCompleteTextView代码进行跟踪,但是找不到触发事件处理程序的代码在哪里。我已经确认处理程序 已注册。

我看了可以在网上找到的ACTV的每个示例。我看了很多关于此的SO帖子,但都没有帮助或指出重点。

package com.foobar.weightlifting.ui

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.MutableLiveData
import com.foobar.weightlifting.R
import kotlinx.android.synthetic.main.lift_selector.*

class LiftDialog : DialogFragment(), AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
    companion object {
        private const val TAG = "LiftDialog"
        private const val POSITIVE_LABEL = "Select"
        private const val NEGATIVE_LABEL = "Cancel"
    }
    private val model = Model(MutableLiveData())
    private lateinit var that: LiftDialog

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        that = this
        return activity?.let {
            val inflater = requireActivity().layoutInflater
            val binding : com.foobar.weightlifting.databinding.LiftSelectorBinding = DataBindingUtil.inflate(inflater, R.layout.lift_selector, null, false)
            binding.model = model
            val view = binding.root

            // Setup autocomplete search bar
            val liftChoices = resources.getStringArray(R.array.lift_list)
            val adapter = ArrayAdapter<String>(context, R.layout.lift_selector, liftChoices)
            val autocompleteView: AutoCompleteTextView = view.findViewById(R.id.lift_search_bar)
            autocompleteView.setAdapter(adapter)
//            autocompleteView.onItemSelectedListener = that
//            autocompleteView.onItemClickListener = that

            autocompleteView.setOnItemClickListener { parent, view, position, id ->
                Log.d(TAG, "info: $position $id")
            }

            val builder = AlertDialog.Builder(it)
            builder.apply {
                setTitle("Describe the lift")
                setView(view)
                setPositiveButton(POSITIVE_LABEL) { dialog, which ->
                    Log.d(TAG, "user pressed the $POSITIVE_LABEL button: $which ; model: ${model.searchText.value}")
                }
                setNegativeButton(NEGATIVE_LABEL) { dialog, which ->
                    Log.d(TAG, "user pressed the $NEGATIVE_LABEL button: $which")
                }
            }
            builder.create()
        } ?: throw IllegalStateException("Blah")
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    data class Model(val searchText: MutableLiveData<String>)
}

和布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="model"
                  type="com.foobar.weightlifting.ui.LiftDialog.Model"/>
    </data>
        <AutoCompleteTextView android:layout_width="wrap_content"
                              android:id="@+id/lift_search_bar"
                              android:text="@={model.searchText}"
                              android:hint="@string/select_lift_hint"
                              android:dropDownWidth="match_parent"
                              android:layout_height="wrap_content"/>
</layout>

如果我在onItemClick中设置了一个断点,我希望当我从自动完成下拉菜单中单击某个项目时,该断点将中断。相反,只有寂寞的D/EGL_emulation: eglMakeCurrent: 0xea5462e0: ver 3 0 (tinfo 0xcf6cc040)被记录下来。

我试图跟踪AutoCompleteTextView代码,但我什至找不到我已注册的onItemClick事件处理程序的调用位置。我已经确认事件处理程序 已在AutoCompleteTextView实例中注册。

2 个答案:

答案 0 :(得分:0)

默认情况下,TextView的android:clickable属性值为false。您必须通过将以下行放入xml中使其可点击:

remove

编辑: 您可以在此link

中找到详细信息

听众将按降序呼叫。您可以设置视图的OnFocusChangeListener。

答案 1 :(得分:0)

创建适配器时,您引用的布局应该是单个自动建议项目的布局,而不是包含ACTV的布局。

因此将实例化适配器的行更改为:

val adapter = ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, liftChoices)

解决了该问题。这使用内置的Android示例下拉布局模板。