Android 微调器未填充 | Android 科特林
Android Spinner Not Populating | Android Kotlin
我是 android 和 kotlin 的新手,所以请原谅我解决的问题可能非常简单!
我使用导航架构组件创建了一个基本应用程序,使用带有三个导航选项的底部导航栏。每个导航选项都指向一个仅显示文本框的专用片段。到目前为止一切顺利,一切正常。
在其中一个片段 (fragement_record.xml/RecordFragmet.kt) 上,我正在尝试加载一个微调器,我想从我定义的字符串数组中填充它strings.xml 文件。当我构建 运行 应用程序时,它可以正常工作,但可见的微调器不包含任何数据 - 它是空的。
我研究过 Stack Overflow 和许多其他网站,但不清楚我做错了什么。
我的字符串数组如下所示:
<string-array name="shot_type_values">
<item>Select shot type…</item>
<item>Tee</item>
<item>Approach</item>
<item>Short</item>
<item>Putt</item>
</string-array>
我的布局文件,fragment_record.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecordFragment">
<Spinner
android:id="@+id/shot_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:background="@color/colorText"
android:minHeight="70dp"
android:visibility="visible" />
</FrameLayout>
我的activity文件,RecordFragment.kt如下图:
package digitalaidconsulting.com.proshotpoc
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.SpinnerAdapter
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_record.*
class RecordFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val shotType = view?.findViewById<Spinner>(R.id.shot_type)
this.context?.let {
ArrayAdapter.createFromResource(
it,
R.array.shot_type_values,
android.R.layout.simple_list_item_1
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
if (shotType != null) {
shotType.adapter = adapter
}
}
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_record, container, false)
}
}
这是构建成功后的最终结果运行(模拟器):
感谢所有帮助!
此时您的观点还没有膨胀。
您可以将代码移至 onViewCreated 方法或使用按以下方式创建的视图:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_record, container, false)
val shotType = view?.findViewById<Spinner>(R.id.shot_type)
this.context?.let {
ArrayAdapter.createFromResource(
it,
R.array.shot_type_values,
android.R.layout.simple_list_item_1
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
if (shotType != null) {
shotType.adapter = adapter
}
}
}
return view
}
请尝试以下代码:-
var adapter = ArrayAdapter<String>(it, android.R.layout.simple_spinner_item, yourArrayList);
shotType.adapter = adapter
注意:另外,请将代码移至 onViewCreated()
重写方法。
我是 android 和 kotlin 的新手,所以请原谅我解决的问题可能非常简单!
我使用导航架构组件创建了一个基本应用程序,使用带有三个导航选项的底部导航栏。每个导航选项都指向一个仅显示文本框的专用片段。到目前为止一切顺利,一切正常。
在其中一个片段 (fragement_record.xml/RecordFragmet.kt) 上,我正在尝试加载一个微调器,我想从我定义的字符串数组中填充它strings.xml 文件。当我构建 运行 应用程序时,它可以正常工作,但可见的微调器不包含任何数据 - 它是空的。
我研究过 Stack Overflow 和许多其他网站,但不清楚我做错了什么。
我的字符串数组如下所示:
<string-array name="shot_type_values">
<item>Select shot type…</item>
<item>Tee</item>
<item>Approach</item>
<item>Short</item>
<item>Putt</item>
</string-array>
我的布局文件,fragment_record.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecordFragment">
<Spinner
android:id="@+id/shot_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:background="@color/colorText"
android:minHeight="70dp"
android:visibility="visible" />
</FrameLayout>
我的activity文件,RecordFragment.kt如下图:
package digitalaidconsulting.com.proshotpoc
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.SpinnerAdapter
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.fragment_record.*
class RecordFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val shotType = view?.findViewById<Spinner>(R.id.shot_type)
this.context?.let {
ArrayAdapter.createFromResource(
it,
R.array.shot_type_values,
android.R.layout.simple_list_item_1
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
if (shotType != null) {
shotType.adapter = adapter
}
}
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_record, container, false)
}
}
这是构建成功后的最终结果运行(模拟器):
感谢所有帮助!
此时您的观点还没有膨胀。
您可以将代码移至 onViewCreated 方法或使用按以下方式创建的视图:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_record, container, false)
val shotType = view?.findViewById<Spinner>(R.id.shot_type)
this.context?.let {
ArrayAdapter.createFromResource(
it,
R.array.shot_type_values,
android.R.layout.simple_list_item_1
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
if (shotType != null) {
shotType.adapter = adapter
}
}
}
return view
}
请尝试以下代码:-
var adapter = ArrayAdapter<String>(it, android.R.layout.simple_spinner_item, yourArrayList);
shotType.adapter = adapter
注意:另外,请将代码移至 onViewCreated()
重写方法。