0
votes

I am trying to save List in room database using type converter and its showing me error

ERROR - error: Class is referenced as a converter but it does not have any converter methods. public class OwnerHistory { ^

My entity Class -

public class OwnerHistory {

@TypeConverters(ListHistoryConverter.class)
@ColumnInfo(name = "listhistory")
@SerializedName("listhistory")
public List<History> history;
public String icon;
public String iconUrl;
public String text;

public OwnerHistory(List<History> history, String icon, String iconUrl, String text) {
    this.history = history;
    this.icon = icon;
    this.iconUrl = iconUrl;
    this.text = text;
}

}

Histor.java

public class History implements Serializable {

public String city;
public String endOwnershipDate;
public int ownerNumber;
public String purchaseDate;
public String state;
public String date;
public String description;
public int odometerReading;
public String source;
public int averageMilesPerYear;
public String useType;

public History(String city, String endOwnershipDate, int ownerNumber, String purchaseDate, String state, String date, String description, int odometerReading, String source, int averageMilesPerYear, String useType) {
    this.city = city;
    this.endOwnershipDate = endOwnershipDate;
    this.ownerNumber = ownerNumber;
    this.purchaseDate = purchaseDate;
    this.state = state;
    this.date = date;
    this.description = description;
    this.odometerReading = odometerReading;
    this.source = source;
    this.averageMilesPerYear = averageMilesPerYear;
    this.useType = useType;
}

}

TypeConverter -

public class ListHistoryConverter {

@TypeConverter // note this annotation
public static String fromOptionValuesList(List<History> optionValues) {
    if (optionValues == null) {
        return (null);
    }
    Gson gson = new Gson();
    Type type = new TypeToken<List<History>>() {
    }.getType();
    String json = gson.toJson(optionValues, type);
    return json;
}

@TypeConverter // note this annotation
public static List<History> toOptionValuesList(String optionValuesString) {
    if (optionValuesString == null) {
        return (null);
    }
    Gson gson = new Gson();
    Type type = new TypeToken<List<History>>() {
    }.getType();
    List<History> productCategoriesList = gson.fromJson(optionValuesString, type);
    return productCategoriesList;
}

}

1
can you add some more info about the error you are getting - rahat

1 Answers

0
votes

According to the documentation, both methods in ListHistoryConverter shouldn't be static (just remove static keywords). Also @TypeConverters(ListHistoryConverter.class) annotation has to be on the class implementing RoomDatabase. I also don't see @Database and @Entity annotations.

So, either you didn't post the whole code, or you better go through all steps from the Room documentation precisely.