2017-10-05 3 views
1

J'utilise Room comme solution de base de données locale dans mon projet. Pour chaque liste d'un certain type d'objet que j'ai ajouté des convertisseurs de type au projet de sorte type convertisseur ressemblerait à quelque chose comme ceci:Android: Comment rendre les convertisseurs de type (pour Room) génériques pour tous Liste d'objets dans Kotlin

@TypeConverter 
fun convertListToString(video: List<VideoType>): String { 

    val videoArray = arrayOfNulls<VideoType>(video.size) 
    for (i in 0..video.size - 1) { 
     videoArray[i] = video[i] 
    } 
    var str = "" 
    val gson = Gson() 
    for (i in videoArray.indices) { 
     val jsonString = gson.toJson(videoArray[i]) 
     str = str + jsonString 
     if (i < videoArray.size - 1) { 
      str = str + strSeparator 
     } 
    } 

    return str 
} 

@TypeConverter 
fun convertStringToList(videoString: String): List<VideoType> { 

    val videoArray = videoString.split(strSeparator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() 
    val videos = ArrayList<VideoType>() 
    val gson = Gson() 
    for (i in 0 until videoArray.size - 1) { 
     videos.add(gson.fromJson(videoArray[i], VideoType::class.java)) 
    } 

    return videos 
} 

problème est seulement que j'ai un tas de liste des différents types que besoin d'être converti, donc actuellement je copie juste ce code pour chaque type. Je voudrais utiliser des génériques, mais jusqu'à présent, je n'ai pas réussi à comprendre comment le faire.

Par exemple en utilisant quelque chose comme:

@TypeConverter 
inline fun <reified T> convertStringToList(string: String): List<T> { 
    val objectArray = string.split(strSeparator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() 
    val objects = ArrayList<T>() 
    val gson = Gson() 
    for (i in 0 until objectArray.size - 1) { 
     objects.add(gson.fromJson(objectArray[i], T::class.java)) 
    } 
    return objects 
} 

ne fonctionne pas et provoque une erreur de compilation avec Android studio me donner un révélateur d'erreur: convertisseurs de type doivent être publics

Toute une idée comment je peut utiliser des génériques pour mon Room TypeConverter?

Répondre

0

Je viens de tester cela, semble faire la base pour ce que vous cherchez. À vous de modifier la fonction à vos besoins, mais vous devriez aller: Référence https://kotlinlang.org/docs/reference/generics.html#generic-functions

fun test() { 
     val list1 = listOf("1", "2") 
     val list2 = listOf(1, 2, 3) 

     list1.convertToString() 
     list2.convertToString() 
    } 

    fun List<Any>.convertToString(): String { 
     var result = "" 
     val gson = Gson() 
     for (i in 0.. size - 1) { 
      result += gson.toJson(get(i), get(i).javaClass) 
     } 
     return result 
    } 
+1

Cela pourrait faire l'affaire de convertir une liste de tout type dans une chaîne, mais il ne résout pas le problème pour l'utilisation de typeConvertors avec Room. Si j'utilise une extension sur une liste je veux convertir en chaîne (même en l'annotant avec @typeConverter) Room; "Impossible de comprendre comment enregistrer ce champ dans la base de données." –