微调框不显示项目

时间:2018-12-06 16:45:26

标签: android android-spinner

我正在尝试在代码中添加微调框。我认为已经正确了。我只想在微调器中显示数据。我正在尝试将其放入数组或从数组字符串中获取,但始终无法正常工作

这是我添加微调器的代码

val spinnerItems = resources.getStringArray(R.array.league_event)
    val myStrings = arrayOf("One", "Two", "Three", "Four", "Five")
    Log.v("spinner", ""+myStrings)
    val spinnerAdapter = ArrayAdapter(requireActivity(),
            android.R.layout.simple_spinner_dropdown_item,
            myStrings)
    lastSpinner?.adapter = spinnerAdapter

我试图将其添加到片段中。请帮助,如果有人知道

3 个答案:

答案 0 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div>
  <form action="" method="post" autocomplete="off" id="theForm">
    <input type="text" name="a" value="some">
    <input type="text" name="b" value="thing">
    <button type="submit" name="SeguA" />
      Seguir
    </button> 
  </form>
</div>

//使用val myStrings = arrayOf("One", "Two", "Three", "Four", "Five") 关键字代替getactivity

requireactivity()

尝试一下。希望它将在微调器中显示数据...

答案 1 :(得分:0)

在Kotlin中,您可以直接访问“活动”,例如:

mySpinner.adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_dropdown_item, myStrings)

答案 2 :(得分:0)

val spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.days, R.layout.spinner_item)
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spnDays.adapter = spinnerAdapter
        spnDays.onItemSelectedListener = this
        spnDays.setSelection(0)

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:padding="@dimen/_5sdp"
    android:textColor="#ffffffff"
    android:textSize="@dimen/_12ssp" />

string.xml

 <string-array name="days">
        <item>TODAY</item>
        <item>YESTERDAY</item>
        <item>TOMORROW</item>
    </string-array>

最后实现接口OnItemSelectedListener