自定义 Alertdialog 中的下拉列表不显示任何项目 (Kotlin)
Dropdown in custom Alertdialog doesn't show any items (Kotlin)
我想创建一个带有下拉列表和其他一些东西的自定义 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>
下面是我在 activity 中调用对话框的方式:
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 出现,我可以看到并单击下拉菜单的箭头,但是当我单击它时没有任何反应。
您在当前 Activity 上呼叫 findViewById
,其中不包含 R.id.pizza_selection
。因此我怀疑你会看到
val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
return 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()
我想创建一个带有下拉列表和其他一些东西的自定义 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>
下面是我在 activity 中调用对话框的方式:
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 出现,我可以看到并单击下拉菜单的箭头,但是当我单击它时没有任何反应。
您在当前 Activity 上呼叫 findViewById
,其中不包含 R.id.pizza_selection
。因此我怀疑你会看到
val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
return 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()