Getting the error error: Not sure how to convert a Cursor to this method's return type

Viewed 786

I need some help to resolve the following error

error: There is a problem with the query: The prepared statement has been finalized List getall();

error: Not sure how to convert a Cursor to this method's return type (java.util.List<com.hashmac.store.dao.CartOffline>). List getall();

error: Must have exactly 1 query in the value of @Query or @DatabaseView List getCartProduct(String priceUnitId);

My Code is:

package com.hashmac.store.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

@Dao
public interface CartDao {

    @Insert
    void insertNew(CartOffline cartOffline);

    @Query("")
    List<CartOffline> getall();

    @Query("")
    List<CartOffline> getCartProduct(String priceUnitId);

    @Query("")
    void updateObj(long quantity, String priceunitid);


    @Query("")
    void deleteObjbyPid(String priceunitid);

    @Delete
    void deleteObj(CartOffline cartOffline);

    @Query("")
    int getProductCount(String pId);

    @Query("")
    void deleteAll();
    /*@Query("UPDATE CartOffline SET quantity = :quantity")
    void updetCart()*/

}
2 Answers

error: Not sure how to convert a Cursor to this method's return type (java.util.List<com.hashmac.store.dao.CartOffline>). List getall();

This means you are trying to query the database directly for a CartOffline object without providing a TypeConverter. Room can only convert complex data to a SQLite primitive type, i.e. TEXT if you provide a TypeConverter.

More info: Referencing complex data using Room

You have to create a class that contains methods that convert the type returned (or taken as parameter) by your DAO queries into SQLite primitive equivalents and vice-versa, so Room knows how to convert a Cursor to your DAO query's return/parameter type.

For example, your getCartProduct() query returns List, so you probably want to parse that into a JSON String that will get stored as SQLite TEXT (you could also parse it differently and store it as BLOB). You can use JSON libraries like Moshi or GSON for that.

(Apologies if there are any typos in the code, but I switched to Kotlin some time ago and my Java is getting rusty.)

 public static class Converters {
   @TypeConverter
   public List<CartOffline> fromJSON(String json) {

     //here goes code to parse a JSON String into a List<CartOffline>
     //using your JSON library of choice
   
     return listOfCartOffline;
   }

   @TypeConverter
   public String fromListOfCartOffline(List<CartOffline> listOfCartOffline) {

     //here goes code to parse List<CartOffline> into a JSON string
     //using your JSON library of choice
   
     return json;
   }
  }

By annotating those methods with @TypeConverter, Room automatically knows how to convert your objects into SQLite types. You will need a converter pair for every non-primitive type that your DAO methods return or take as parameter. If your queries only return primitives like Strings, Integers, or collections of them (like List), you should not need a @TypeConverter.

I have written a kotlin extension to handle this for me so I'm hoping it helps. I use Moshi converter so you may want to consider your options.

/**
 * [Moshi] extension to transform a [List] to Json
 * */
inline fun <reified T> Moshi.listToJson(data: List<T>): String =
    adapter<List<T>>(
        Types.newParameterizedType(
            List::class.java, T::class.java
        )
    ).toJson(data)

/**
 * [Moshi] extension to transform a json object to a [List]
 * */
inline fun <reified T> Moshi.jsonToList(json: String): List<T>? =
    adapter<List<T>>(
        Types.newParameterizedType(
            List::class.java, T::class.java
        )
    ).fromJson(json)

For your usage, create an instance of Moshi and then transform json to List using

moshi.jsonToList(json)

and

moshi.listToJson(listOfCartOffline)
Related