【问题标题】:Dropdown in custom Alertdialog doesn't show any items (Kotlin)自定义 Alertdialog 中的下拉菜单不显示任何项目(Kotlin)
【发布时间】:2021-06-15 11:35:15
【问题描述】:

我想创建一个带有下拉列表和其他一些东西的自定义 Alertdialog 布局。我正在使用 Kotlin,我对它很陌生 目前我被困在下拉列表中,因为它没有显示任何内容

这是 Layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/backButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="Zurück"
        app:icon="@drawable/ic_baseline_arrow_back_24"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/pizzaSelection"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我在我的活动中调用对话框的方式:

   private fun showPizzaDialog(){
        val pizzaDialogBuilder = AlertDialog.Builder(this)
        pizzaDialogBuilder.setView(R.layout.pizza_alertdialog)
        
        val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
        val pizzaTypes = resources.getStringArray(R.array.pizzaTypes)

        if (pizzaDropdown != null) {
            val adapter = ArrayAdapter(this,
                android.R.layout.simple_spinner_item, pizzaTypes)
            pizzaDropdown.adapter = adapter
        }
        
        pizzaDialogBuilder.show()

    }

项目目前在 strings.xml 资源文件中硬编码为字符串数组。

AlertDialog 出现了,我可以看到并单击下拉菜单的箭头,但是当我单击它时没有任何反应。

【问题讨论】:

    标签: android kotlin dropdown spinner


    【解决方案1】:

    您在当前 Activity 上调用 findViewById,该 Activity 不包含 R.id.pizza_selection。因此我怀疑你会看到

    val pizzaDropdown = findViewById&lt;Spinner&gt;(R.id.pizzaSelection)

    返回null

    试试这样的:

    // inflate your layout    
    val dialogView = LayoutInflater.from(this).inflate(R.layout.pizza_alert_dialog, null, false)
         
    // and set it as dialog view          
    pizzaDialogBuilder.setView(dialogView)
        
    // then call findViewById on this ViewGroup to get the Spinner            
    val pizzaDropdown = dialogView.findViewById<Spinner>(R.id.pizzaSelection)
    

    我的 Kotlin 语法可能不正确,抱歉。重要的是我们在dialogView 上调用findViewById,而不是隐含的this.findViewById()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2015-01-10
      相关资源
      最近更新 更多