1

Parsing a nested JSON data with gson and saving it in local db using room. However, I keep getting the following exception

error: Not sure how to convert a Cursor to this method's return type (kotlinx.coroutines.flow.Flow<java.util.TreeMap<java.lang.String,java.util.ArrayList<com.Category>>>).

Would need some help in solving this issue. Can anyone point out what am I missing Thanks!

response.json

{
"data": {
    "Category1": [
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        }
    ],
    "Category2": [
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        }
    ],
    "Category3": [
       {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        },
        {
            "data": "xyz",
            "value": "",
            "type": "",
            "title": ""
        }
    ]}}

Following is my response class

data class CategoryShowResponse (


@SerializedName("data")
val categoryShowResponse: TreeMap<String,ArrayList<Category>>)

Below is my Category class

 @Entity(tableName = TABLE_NAME)
 data class Category(
 @SerializedName( "data")  val data: String,
 @SerializedName("value")  val value: String,
 @SerializedName( "type")  val type: String,
 @SerializedName( "title")  val title: String) {
   companion object {
    const val TABLE_NAME = "Category_data"
   }}

Below is in AppDatabase.class

@Database(
entities = [Category::class],
version = DatabaseMigrations.DB_VERSION)
 abstract class _Database: RoomDatabase() {

 abstract fun getCategorizedDao(): CategorizedDao}

Finally my CategoryDao class

@Dao
abstract class CategoryDao {


@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insertCategorizedShows(shows: TreeMap<String, ArrayList<Category>>)


@Query("DELETE FROM ${Category.TABLE_NAME}")
abstract fun deleteAllCategories()

@Query("SELECT * FROM ${Category.TABLE_NAME}")
abstract fun getAllCategories(): Flow<TreeMap<String, ArrayList<Category>>>}

I keep getting the following exception

error: Not sure how to convert a Cursor to this method's return type (kotlinx.coroutines.flow.Flow<java.util.TreeMap<java.lang.String,java.util.ArrayList<com.Category>>>).

Would need some help in solving this issue

goonerDroid
  • 10,527
  • 8
  • 34
  • 51
  • Does this answer your question? [Room "Not sure how to convert a Cursor to this method's return type": which method?](https://stackoverflow.com/questions/46445964/room-not-sure-how-to-convert-a-cursor-to-this-methods-return-type-which-meth) – a_local_nobody Sep 03 '20 at 13:46
  • 1
    i think you should change your question to specifically ask for a Treemap converter for room, instead of asking why the current code doesn't work, then at least it isn't a duplicate or similar – a_local_nobody Sep 03 '20 at 13:52

0 Answers0