1

I am trying to pass a string-array from function to activity. I want to use that array into the spinner. But when I am trying to do this, I am passing an array but the function wants to pass a array of type Int. But in activity array of Array<String> type is required.

here is the code of the activity:-

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<*>?) {
            }
        }
}

here is function code:-

object RegionSelectionFunctions {

    fun selectedStateForSmallRegions(state: String): SelectingDistrictLocationArrayForSmallRegions {
        when (state) {
            "-- Select State --" -> return SelectingDistrictLocationArrayForSmallRegions(R.array.empty,View.GONE, View.VISIBLE, View.GONE)
        }
    }

here is the code of the data class:-

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

I have tried all the possible ways I know to solve this. Can someone please help me?

9
  • Maybe in the first line of selectingDistrictLocation() instead of districtArray: Int you should have districtArray: Array<String>. This is what you want, right? Commented Jun 25, 2022 at 2:40
  • Yes I want exactly the same thing. Commented Jun 25, 2022 at 2:50
  • Did it fix your problem? Commented Jun 25, 2022 at 3:19
  • Already I have used districtArray: Array<String>. It's not working. Commented Jun 25, 2022 at 3:22
  • 1
    No, you haven't. in your selectingDistrictLocation() function in the activity, you have used val districArray: Int. it should change to val districtArray: Array<String> Commented Jun 25, 2022 at 3:36

1 Answer 1

1

It is not possible to convert java.lang.Integer cannot be cast to java.lang.String[].

Refer to this question.

For doing the same thing you can pass the string as a value for each case. Then compare that case in the activity. Assign the values of the arrays in the activity.

Sign up to request clarification or add additional context in comments.

1 Comment

This can be my last option to solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.