Android 房间持久库 - TypeConverter error of error:无法弄清楚如何将字段保存到数据库" [英] Android room persistent library - TypeConverter error of error: Cannot figure out how to save field to database"

查看:35
本文介绍了Android 房间持久库 - TypeConverter error of error:无法弄清楚如何将字段保存到数据库"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于错误,我无法在房间内创建 typeConverter.我似乎正在按照文档中的所有内容进行操作.我想将列表转换为 json 字符串.让我们看看我的实体:

Im not able to create a typeConverter in room due to an error. I seem to be following everything per the docs. I would like to convert a list to a json string. lets take a look at my entity:

      @Entity(tableName = TABLE_NAME)
public class CountryModel {

    public static final String TABLE_NAME = "Countries";

    @PrimaryKey
    private int idCountry;
/* I WANT TO CONVERT THIS LIST TO A JSON STRING */
    private List<CountryLang> countryLang = null;

    public int getIdCountry() {
        return idCountry;
    }

    public void setIdCountry(int idCountry) {
        this.idCountry = idCountry;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

    public List<CountryLang> getCountryLang() {
        return countryLang;
    }

    public void setCountryLang(List<CountryLang> countryLang) {
        this.countryLang = countryLang;
    }

}

country_lang 是我想转换为字符串 json 的内容.所以我创建了以下转换器:Converters.java:

The country_lang is what i would like to convert to a string json. So i created the following converter: Converters.java:

public class Converters {

@TypeConverter
public static String countryLangToJson(List<CountryLang> list) {

    if(list == null)
        return null;

        CountryLang lang = list.get(0);

    return list.isEmpty() ? null : new Gson().toJson(lang);
}}

然后问题出在我放置@TypeConverters({Converters.class}) 的任何地方,我一直收到错误消息.但正式这是我放置注释以注册 typeConverter 的地方:

then the problem is anywhere i put the @TypeConverters({Converters.class}) i keep getting an error. But officially this is where i have placed the annotation to get the typeConverter registered:

@Database(entities = {CountryModel.class}, version = 1 ,exportSchema = false)
@TypeConverters({Converters.class})
public abstract class MYDatabase extends RoomDatabase {
    public abstract CountriesDao countriesDao();
}

我得到的错误是:

Error:(58, 31) error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.

推荐答案

这是自 Room 发布以来我看到的一个常见问题.Room 不支持直接存储 Lists 的功能,也不支持在 Lists 之间进行转换.支持POJO的转换和存储.

This is a common problem I've seen since Room was announced. Room does not support the ability to store Lists directly, nor the ability to convert to/from Lists. It supports converting and storing POJO's.

在这种情况下,解决方案很简单.不是存储 List,而是要存储 CountryLangs(注意 's')

In this case the solution is simple. Instead of storing a List<CountryLang> you want to store CountryLangs (note the 's')

我在这里做了一个简单的解决方案示例:

I've done a quick example of a solution here :

public class CountryLangs {
    private List<String> countryLangs;

    public CountryLangs(List<String> countryLangs) {
        this.countryLangs = countryLangs;
    }

    public List<String> getCountryLangs() {
        return countryLangs;
    }

    public void setCountryLangs(List<String> countryLangs) {
        this.countryLangs = countryLangs;
    }
}

这个 POJO 是你之前对象的反转.它是一个存储语言列表的对象.而不是存储您的语言的对象列表.

This POJO is an inversion of your previous object. It is an object that stores a list of languages. Instead of a list of objects that store your language.

public class LanguageConverter {
    @TypeConverter
    public CountryLangs storedStringToLanguages(String value) {
        List<String> langs = Arrays.asList(value.split("\s*,\s*"));
        return new CountryLangs(langs);
    }

    @TypeConverter
    public String languagesToStoredString(CountryLangs cl) {
        String value = "";

        for (String lang :cl.getCountryLangs())
            value += lang + ",";

        return value;
    }
}

这个转换器接受一个字符串列表,并将它们转换成逗号分隔的字符串以存储在单个列中.当它从 SQLite 数据库中获取字符串以转换回来时,它会用逗号分割列表,并填充 CountryLangs.

This converter takes a list of strings and converts them into a comma seperated string to be stored in a single column. When it fetches the string from the SQLite db to convert back, it splits the list on commas, and populates the CountryLangs.

确保在进行这些更改后更新 RoomDatabase 版本.其余配置正确.其余的 Room 持久性工作祝您狩猎愉快.

Insure to update your RoomDatabase version after making these changes.You have the rest of the configuration correct. Happy hunting with the rest of your Room persistence work.

这篇关于Android 房间持久库 - TypeConverter error of error:无法弄清楚如何将字段保存到数据库"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆