0
\$\begingroup\$

I managed to setup multiple spinners using one listener but there is a lot of repetition in the code.

My full onItemSelected method:

 override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
    when (parent.id) {
        R.id.spinner_ingredient_1 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement1 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
        R.id.spinner_ingredient_2 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement2 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }

        }
        R.id.spinner_ingredient_3 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement3 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
        R.id.spinner_ingredient_4 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement4 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
        R.id.spinner_ingredient_5 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement5 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
        R.id.spinner_ingredient_6 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement6 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
        R.id.spinner_ingredient_7 -> {
            val selection = parent.getItemAtPosition(position) as String
            mMeasurement7 = when (selection) {
                getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
                getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
                getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
                getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
                getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
                getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
                else -> DataEntry.MEASUREMENT_OZ
            }
        }
    }
}

I made a slight improvement since my original post but I think it can be improved. Please let me know your suggestions!

 // Setup the dropdown spinner that allows the user to select the quantity for each ingredient.
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
    // Checks the views in the adapter which are the views I associated to the adapter within
    // onCreate
    val measurementSpinnerOptions = when (parent.getItemAtPosition(position) as String) {
        getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
        getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
        getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
        getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
        getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
        getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
        else -> DataEntry.MEASUREMENT_OZ
    }
    when (parent.id) {
        R.id.spinner_ingredient_1 -> {
            mMeasurement1 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_2 -> {
            mMeasurement2 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_3 -> {
            mMeasurement3 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_4 -> {
            mMeasurement4 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_5 -> {
            mMeasurement5 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_6 -> {
            mMeasurement6 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_7 -> {
            mMeasurement7 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_8 -> {
            mMeasurement8 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_9 -> {
            mMeasurement9 = measurementSpinnerOptions
        }
        R.id.spinner_ingredient_10 -> {
            mMeasurement10 = measurementSpinnerOptions
        }
    }
}


// Required function for onItemSelected.
override fun onNothingSelected(p0: AdapterView<*>?) {
    mMeasurement1 = DataEntry.MEASUREMENT_OZ
    mMeasurement2 = DataEntry.MEASUREMENT_OZ
    mMeasurement3 = DataEntry.MEASUREMENT_OZ
    mMeasurement4 = DataEntry.MEASUREMENT_OZ
    mMeasurement5 = DataEntry.MEASUREMENT_OZ
    mMeasurement6 = DataEntry.MEASUREMENT_OZ
    mMeasurement7 = DataEntry.MEASUREMENT_OZ
    mMeasurement8 = DataEntry.MEASUREMENT_OZ
    mMeasurement9 = DataEntry.MEASUREMENT_OZ
    mMeasurement10 = DataEntry.MEASUREMENT_OZ

}
\$\endgroup\$
  • \$\begingroup\$ Is there any reason why you can't just use smth like fun DataEntry.from(selection: String) = when (selection) { ... }? Wait... why are you even switching on the id? The code inside the case looks identical (unless I'm missing something; I'm not an Android person.) \$\endgroup\$ – CAD97 Sep 22 '17 at 20:12
1
\$\begingroup\$

Repetition

I have not tested this on Android, but I think something like this should work, storing the individual units in an array.

You can use getters if you want to still access them as measurementN

class T: Listener {
    val measurements = MutableList(7, {DataEntry.MEASUREMENT_OZ})

    override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
        when (parent.id) {
            R.id.spinner_ingredient_1 -> update(0, position, parent)
            R.id.spinner_ingredient_2 -> update(1, position, parent)
            R.id.spinner_ingredient_3 -> update(2, position, parent)
            R.id.spinner_ingredient_4 -> update(3, position, parent)
            R.id.spinner_ingredient_5 -> update(4, position, parent)
            R.id.spinner_ingredient_6 -> update(5, position, parent)
            R.id.spinner_ingredient_7 -> update(6, position, parent)
        }
    }

    fun update(item: Int, position: Int, parent: AdapterView<*>) {
        val selection = parent.getItemAtPosition(position)
        measurements[item] = when (selection) {
            getString(R.string.measurement_oz) -> DataEntry.MEASUREMENT_OZ
            getString(R.string.measurement_ml) -> DataEntry.MEASUREMENT_ML
            getString(R.string.measurement_dash) -> DataEntry.MEASUREMENT_DASH
            getString(R.string.measurement_drop) -> DataEntry.MEASUREMENT_DROP
            getString(R.string.measurement_cube) -> DataEntry.MEASUREMENT_CUBE
            getString(R.string.measurement_garnish) -> DataEntry.MEASUREMENT_GARNISH
            else -> DataEntry.MEASUREMENT_OZ
        }
    }
}

For completeness I have included the shim I used below

class AdapterView<T> {
    val id: Long = -1
    fun getItemAtPosition(id: Int): String {
        return "MOCKED"
    }
}
class View
interface Listener {
    fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long)
}

class R {
    object id {
        val spinner_ingredient_1 = 0L
        val spinner_ingredient_2 = 1L
        val spinner_ingredient_3 = 2L
        val spinner_ingredient_4 = 3L
        val spinner_ingredient_5 = 4L
        val spinner_ingredient_6 = 5L
        val spinner_ingredient_7 = 6L
    }
    object string {
        val measurement_oz = 0L
        val measurement_ml = 1L
        val measurement_dash = 2L
        val measurement_drop = 3L
        val measurement_cube = 4L
        val measurement_garnish = 5L
    }
}

enum class DataEntry{
    MEASUREMENT_OZ,
    MEASUREMENT_ML,
    MEASUREMENT_DASH,
    MEASUREMENT_DROP,
    MEASUREMENT_CUBE,
    MEASUREMENT_GARNISH
}

fun getString(id: Long): String {
    return "MOCK"
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Honestly your answer is way above my skill level so I had to study this for a bit. I was able to use your code to simply. Thanks! \$\endgroup\$ – Dallas Sep 23 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.