首页 > 解决方案 > Android Spinner 未填充 | 安卓科特林

问题描述

我对 android 和 kotlin 很陌生,所以请原谅可能是一个非常简单的问题要解决!

我使用导航架构组件创建了一个基本应用程序,使用底部导航栏和三个导航选项。每个导航选项都指向一个仅显示文本框的专用片段。到目前为止一切顺利,这一切正常。

在其中一个片段 (fragement_record.xml/RecordFragmet.kt) 上,我正在尝试加载一个我想从我的 strings.xml 文件中定义的字符串数组中填充的单个微调器。当我构建并运行该应用程序时,它可以正常工作,但可见的微调器不包含任何数据 - 它是空的。

我研究了 Stack Overflow 和许多其他网站,但不清楚我做错了什么。

我的字符串数组如下所示:

<string-array name="shot_type_values">
    <item>Select shot type…&lt;/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>

我的活动文件 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)

    }
}

这是构建成功运行后的最终结果(模拟器):

应用加载后空微调器的屏幕截图

感谢所有帮助!

标签: androidkotlinfragmentspinner

解决方案


在这一点上,你的观点还没有膨胀。

您可以将代码移动到 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
    }

推荐阅读