エラーのためルームにtypeConverterを作成できません。 JSON文字列にリストを変換するために、私の実体を見てみましょう:。Androidルームの永続ライブラリ - TypeConverterエラー:データベースへのフィールドの保存方法を把握できません "
@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に変換したいものですので、私は、次のコンバータ作成:。 コンバータ。 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を取得するには、注釈を配置しているところである:私は取得
@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.
「文字列」から「リスト」に変換されたコンバータもありますか?私はあなたのコードでそれが表示されません。部屋には、データを読み戻せるようにする必要もあります。 –
yigit
私はそれを試みたが、私は助けなかった。同じエラー – j2emanue
リストのTypeConverterが機能します。それがうまくいかない場合は、逆変換のメソッドを追加することを忘れていると思います。 '@TypeConverter public static List jsonToCountryLang(String json)' –
Gak2