Created
January 29, 2019 14:44
-
-
Save Sammekl/461ebc457df19af09615fcdb499e8095 to your computer and use it in GitHub Desktop.
Spinner extensions Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun AppCompatSpinner.isDefaultSelected() = this.selectedItemPosition == 0 | |
fun AppCompatSpinner.setSelectedValue(value: String?) { | |
setSelection(when (value) { | |
null -> 0 | |
else -> adapter.getPositionOf(value, true) | |
}) | |
} | |
fun SpinnerAdapter.getPositionOf(value: String, ignoreCase: Boolean = true): Int { | |
forEachIndexed { index, spinnerValue -> | |
if (spinnerValue.equals(value, ignoreCase)) { | |
return index | |
} | |
} | |
return 0 | |
} | |
inline fun SpinnerAdapter.forEachIndexed(item: (Int, String) -> Unit) { | |
for (i in 0 until count) { | |
item(i, getItem(i) as String) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment