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?

CHETAN
  • 380
  • 1
  • 3
  • 15
  • Maybe in the first line of `selectingDistrictLocation()` instead of `districtArray: Int` you should have `districtArray: Array`. This is what you want, right? – MehranB Jun 25 '22 at 02:40
  • Yes I want exactly the same thing. – CHETAN Jun 25 '22 at 02:50
  • Did it fix your problem? – MehranB Jun 25 '22 at 03:19
  • Already I have used districtArray: Array. It's not working. – CHETAN Jun 25 '22 at 03: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` – MehranB Jun 25 '22 at 03:36
  • I have tried it earlier. I have tried all possible combinations I know. – CHETAN Jun 25 '22 at 03:38
  • the problem after those changes is still there in `fun selectedStateForSmallRegions()`. The array `R.array.empty` is of type `Int` but required is `Array`. – CHETAN Jun 25 '22 at 05:36
  • by passing R.array.empty, are you trying to pass an empty array in `fun selectedStateForSmallRegions()`? – MehranB Jun 25 '22 at 05:58
  • In this condition yes but in other conditions I will have to pass different string arrays. I have put when condition to pass different arrays. – CHETAN Jun 25 '22 at 06:04

1 Answers1

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.

Prasad v Bhagat
  • 436
  • 2
  • 11