gpt4 book ai didi

android - 微调器 onItemSelected() 没有被调用

转载 作者:IT老高 更新时间:2023-10-28 13:40:40 26 4
gpt4 key购买 nike

我已经实现了一个 ArrayAdapter 来填充我的 Spinner View 。 Spinner 工作正常,但是当我单击微调器中的项目时,android 没有检测到。

我已遵守 spinner example in the Android docs 中的所有要求 包括在我的 Activity 中实现 AdapterView.OnItemSelectedListener 并覆盖它的两个方法 OnItemSelectedListeneronNothingSelected,但是,我的 Log 都没有> 这些方法中的语句会打印出来,因此不会被调用。

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

这是我的 Activity :

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")
}

...

知道问题出在哪里吗?

另外,当我在 Spinner 中选择一个项目时,我的 Spinner 旁边的 View 会移动,因此显然可以检测到它,但仍然没有调用 onItemSelected()

最佳答案

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

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

现在 onItemSelected() 已成功调用。

关于android - 微调器 onItemSelected() 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55278353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com