微调器onItemSelected()不被调用

时间:2019-03-21 10:28:24

标签: android kotlin

我已经实现了ArrayAdapter来填充我的Spinner视图。 Spinner工作正常,但是当我单击微调器中的某个项目时,android并未检测到。

我已经遵守spinner example in the Android docs中的所有要求  包括对我的Activity实施AdapterView.OnItemSelectedListener并覆盖这两个方法OnItemSelectedListeneronNothingSelected,但是,这些方法中的Log语句均未打印,因此不会被调用。 / p>

我还通过choose_user.onItemSelectedListener = this@PlayerDetails将监听器设置为微调器。

这是我的活动:

class PlayerDetails : AppCompatActivity(), View.OnClickListener, TextWatcher, AdapterView.OnItemSelectedListener {
    val TAG: String = "PlayerDetails"
    val FirebaseTAG: String = "FirebaseDebug"

    var numOfPlayers: Int = 1
    var currentPlayer: Int = 1

    var name: String = ""
    var age: Int = 0
    var genderId: Int = 0
    var genderResult: String = ""

    val db = FirebaseFirestore.getInstance()
    var users: MutableList<String> = mutableListOf()

    private lateinit var binding: ActivityPlayerDetailsBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val sharedPref = this@PlayerDetails.getPreferences(Context.MODE_PRIVATE)
        val applicationID: String? = sharedPref.getString("applicationID", null)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_player_details)

        if (applicationID != null) {

            db.collection("phones").document(applicationID)
                .collection("users")
                .get()
                .addOnSuccessListener { result ->

                    for (document in result){
                        val name = document.get("name").toString()
                        users.add(name)
                    }
                }

            Log.d(FirebaseTAG, users.toString())
            val spinnerAdaptor = ArrayAdapter<String>(this@PlayerDetails, android.R.layout.simple_spinner_item, users)
            spinnerAdaptor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            choose_user?.adapter = spinnerAdaptor
            choose_user.onItemSelectedListener = this@PlayerDetails
        }

        val intent = getIntent()
        numOfPlayers = intent.getIntExtra("number_of_players", 1)
        next_details.setOnClickListener(this)
        player_name.addTextChangedListener(this)
        player_age.addTextChangedListener(this)
        gender.setOnCheckedChangeListener(object: RadioGroup.OnCheckedChangeListener {
            override fun onCheckedChanged(radiogroup: RadioGroup, checked: Int) {
                if (fieldsArePopulated()) next_details.visibility = View.VISIBLE
            }
        })

    }

    override fun onItemSelected(parent: AdapterView<*>?, name: View?, position: Int, rowId: Long) {
        val chosenName: String = parent?.getItemAtPosition(position).toString()
        Log.d("ChosenName", chosenName)
        Log.d("adapterclicked", "adapterclicked")

    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        Log.d("Nothing", "NOTHINGCALLED")
    }

...

有什么问题吗?

此外,当我在微调器中选择一个项目时,微调器旁边的视图也会移动,因此显然可以检测到它,但仍未调用onItemSelected()

3 个答案:

答案 0 :(得分:2)

我为users MutableList设置了一个初始值:

var users: MutableList<String> = mutableListOf("Choose User")

现在成功调用了onItemSelected()

答案 1 :(得分:0)

我很确定onItemSelected返回数组中该项位置的int值。因此,您必须将其另存为int而不是字符串,然后添加另一个包含字符串和array [intSaved]

的变量

修改

[
  { name: "a"},
  { name: "b"},
  { name: "a"}
]

答案 2 :(得分:0)

尝试

之前:

override fun onItemSelected(parent: AdapterView<*>?, name: View?, position: Int, rowId: Long) {
    val chosenName: String = parent?.getItemAtPosition(position).toString()
    Log.d("ChosenName", chosenName)
    Log.d("adapterclicked", "adapterclicked")

}

之后:

override fun onItemSelected(parent: AdapterView<*>?, name: View?, position: Int, rowId: Long) {
    val chosenNameTextView = parent!!.getChildAt(0) as TextView
    Log.e("MainActivity","choose Spin : ${chosenNameTextView.text.toString()}")
}