【问题标题】:Android Spinner Not Populating | Android KotlinAndroid Spinner 未填充 |安卓科特林
【发布时间】:2020-06-06 15:19:49
【问题描述】:

我对 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>

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

    }
}

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

感谢所有帮助!

【问题讨论】:

    标签: android kotlin fragment spinner


    【解决方案1】:

    此时您的视图尚未膨胀。

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

    【讨论】:

    • 谢谢@sebastian。这非常有效。感谢您的快速响应。
    【解决方案2】:

    请尝试以下代码:-

    var adapter =  ArrayAdapter<String>(it, android.R.layout.simple_spinner_item, yourArrayList);
    shotType.adapter = adapter
    

    注意:另外,请将代码移至 onViewCreated() 重写方法。

    【讨论】:

    • 谢谢@shalu。感谢您的快速响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多