如何使微调器中的 int 值显示为微调器内 UI 中的文本值?

How can I make my int values in spinner display as Text values in my UI inside my spinner?

我的应用程序中有一个性别微调器,其值为(0 和 1)0 = "Male"1 = "Female" 我将它们用作整数,因为这是 POST 中所需要的发送用户信息的方法。我怎样才能将这些值作为 Int 发送,但让它们在我的应用程序中显示为文本?

这是我的activity代码:

class EditUserActivity : AppCompatActivity() {

    var genderSelected = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.edit_user)

        editSpinner.visibility = View.INVISIBLE

        val genderList = arrayOf(0, 1)
        genderSelected = genderList[0]

        val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, genderList)

        genderSpinner.adapter = arrayAdapter

        genderSpinner.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                  genderSelected = genderList.get(position)
            }
        }

        EditFirstName.setText(UserDataService.firstName)
        EditLastName.setText(UserDataService.lastName)
        EditEmail.setText(UserDataService.email)
        EditMobile.setText(UserDataService.mobile)
        EditWhatsapp.setText(UserDataService.whatsapp)
        EditAddress.setText(UserDataService.address)
    }

    fun editButtonClicked(view: View) {
        enableSpinner(true)
        val firstName = EditFirstName.text.toString()
        val lastName = EditLastName.text.toString()
        val email = EditEmail.text.toString()
        val mobile = EditMobile.text.toString()
        val whatsapp = EditWhatsapp.text.toString()
        val address = EditAddress.text.toString()
        val gender = genderSelected

        if (firstName.isNotEmpty() && lastName.isNotEmpty() && email.isNotEmpty() && mobile.isNotEmpty() && address.isNotEmpty() && whatsapp.isNotEmpty()) {
            AuthService.editUserProfile(this, UserDataService.id,email,firstName,lastName,gender,mobile, address, whatsapp) { editSuccess ->
                if (editSuccess) {
                    val userDataChange = Intent(BROADCAST_USER_DATA_CHANGE)
                    LocalBroadcastManager.getInstance(this).sendBroadcast(userDataChange)
                    enableSpinner(false)
                    UserDataService.firstName = firstName
                    UserDataService.lastName = lastName
                    UserDataService.email = email
                    UserDataService.mobile = mobile
                    UserDataService.whatsapp = whatsapp
                    UserDataService.address = address
                    UserDataService.gender = gender.toString()
                    val intent = Intent(this, UserProfile::class.java)
                    startActivity(intent)
                    finish()
                }
                else {
                    errorToast()
                    enableSpinner(false)
                }
            }
        } else {
            Toast.makeText(this, "Make sure the fields are filled.", Toast.LENGTH_LONG).show()
            enableSpinner(false)
        }
    }

    fun errorToast() {
        Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show()
        enableSpinner(false)
    }

    fun enableSpinner(enable: Boolean) {
        if (enable) {
            editSpinner.visibility = View.VISIBLE
            EditButton.isEnabled = false
            EditAddress.isEnabled = false
            EditEmail.isEnabled = false
            EditFirstName.isEnabled = false
            EditLastName.isEnabled = false
            EditWhatsapp.isEnabled = false
            EditMobile.isEnabled = false
            genderSpinner.isEnabled = false
        } else {
            editSpinner.visibility = View.INVISIBLE
            genderSpinner.isEnabled = true
            EditButton.isEnabled = true
            EditFirstName.isEnabled = true
            EditLastName.isEnabled = true
            EditEmail.isEnabled = true
            EditAddress.isEnabled = true
            EditMobile.isEnabled = true
            EditWhatsapp.isEnabled = true
        }
    }
}

值在 arrayOf 中设置,后来在 var genderSelected = 0 中调用,以便在用户提交时通过我的 POST 代码发送该值

这样做,替换你的代码

val genderList = arrayOf("Male", "Female")

当你将它传递给 api 时调用它

val gender = if (genderSelected == "Male")
    0
else
    1