Skip to content

Instantly share code, notes, and snippets.

@Meraj
Last active December 27, 2020 11:27
Show Gist options
  • Save Meraj/88c9770ac6cac3aba4a3cd2ed19e76e8 to your computer and use it in GitHub Desktop.
Save Meraj/88c9770ac6cac3aba4a3cd2ed19e76e8 to your computer and use it in GitHub Desktop.
a simple function that auto convert cursors to array in kotlin - android
/**
* a simple function that convert cursors to String Array
* ArioSql = https://github.com/MerajV/ArioSql
* @property cursor Cursor
* @return Array<Array<String>>
* @author MerajV
* @since 0.3.12
*/
fun convertCursorToArray(cursor: Cursor): Array<Array<String>> {
val getColumnsCount :Int = cursor.columnCount
var cursorArray :Array<Array<String>> = arrayOf<Array<String>>()
var columns :Array<String> = arrayOf<String>()
var indexes = 0
cursor.count
if(cursor.count != 0){
if(cursor.count == 1){
cursor.moveToFirst()
for (i in 0 until getColumnsCount step 1){
val list: MutableList<String> = columns.toMutableList()
list.add(cursor.getString(i))
columns = list.toTypedArray()
}
val list: MutableList<Array<String>> = cursorArray.toMutableList()
list.add(columns)
cursorArray = list.toTypedArray()
}else{
while (cursor.moveToNext()){
for (i in 0 until getColumnsCount step 1){
val list: MutableList<String> = columns.toMutableList()
list.add(cursor.getString(i))
columns = list.toTypedArray()
}
val list: MutableList<Array<String>> = cursorArray.toMutableList()
list.add(columns)
cursorArray = list.toTypedArray()
columns = arrayOf()
indexes++
}
}
}
return cursorArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment