Skip to content

Instantly share code, notes, and snippets.

@droibit
Last active March 5, 2018 04:55
Show Gist options
  • Save droibit/1e212726ad9ba449427b to your computer and use it in GitHub Desktop.
Save droibit/1e212726ad9ba449427b to your computer and use it in GitHub Desktop.
ArrayAdapter<T>内の全Itemを取得するための拡張メソッド
// ArrayAdapter.getItems() with iterator
public class ArrayAdapterIterator<T>(val adapter: ArrayAdapter<T>) {
private var index = 0
fun next() = adapter.getItem(index++)
fun hasNext() = index < adapter.getCount()
}
public fun <T>ArrayAdapter<T>.iterator(): ArrayAdapterIterator<T> = ArrayAdapterIterator(this)
public fun <T>ArrayAdapter<T>.getItems(): List<T> {
val items = ArrayList<T>(getCount())
for (item in this) {
items.add(item)
}
return items
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment