[英]How to pass a string-array from the function to the activity in android kotlin?
我正在尝试将字符串数组从函数传递到活动。 我想将该数组用于微调器。 但是当我尝试这样做时,我正在传递一个数组,但该函数想要传递一个Int
类型的数组。 但在Array<String>
类型的活动数组中是必需的。
这是活动的代码:-
fun selectingDistrictLocation() {
val (districtArray: Int, districtVisibility: Int, stateWarningVisibility: Int, recyclerViewVisibility: Int) = selectedState(SelectedStateName)
district_location.visibility = districtVisibility
state_warning.visibility = stateWarningVisibility
val locationDistrictAdapter = ArrayAdapter(
this,
R.layout.support_simple_spinner_dropdown_item,
districtArray
)
districts_spinner_location.adapter = locationDistrictAdapter
districts_spinner_location.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
adapterViewDistrict: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
DistrictSelectedName =
adapterViewDistrict?.getItemAtPosition(position).toString()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
这是功能代码:-
object RegionSelectionFunctions {
fun selectedStateForSmallRegions(state: String): SelectingDistrictLocationArrayForSmallRegions {
when (state) {
"-- Select State --" -> return SelectingDistrictLocationArrayForSmallRegions(R.array.empty,View.GONE, View.VISIBLE, View.GONE)
}
}
这是数据类的代码:-
data class SelectingDistrictLocationArray(
val DistrictList: Array<String>,
val districtVisibility: Int,
val stateWarningVisibility: Int,
val recyclerViewVisibility: Int) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as SelectingDistrictLocationArray
if (!DistrictList.contentEquals(other.DistrictList)) return false
return true
}
override fun hashCode(): Int {
return DistrictList.contentHashCode()
}
}
我已经尝试了所有可能的方法来解决这个问题。 有人可以帮帮我吗?
无法将java.lang.Integer cannot be cast to java.lang.String[]
。
为了做同样的事情,您可以将字符串作为每个案例的值传递。 然后在活动中比较该案例。 在活动中分配数组的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.