2017-06-16 8 views
7

エラーのためルームに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. 
+0

「文字列」から「リスト」に変換されたコンバータもありますか?私はあなたのコードでそれが表示されません。部屋には、データを読み戻せるようにする必要もあります。 – yigit

+0

私はそれを試みたが、私は助けなかった。同じエラー – j2emanue

+0

リストのTypeConverterが機能します。それがうまくいかない場合は、逆変換のメソッドを追加することを忘れていると思います。 '@TypeConverter public static List jsonToCountryLang(String json)' – Gak2

答えて

6

これはルームが発表されたので、私が見てきた共通の問題です。ルームは、リストを直接格納する機能やリストとの変換機能をサポートしていません。 POJOの変換と保存をサポートしています。

この場合、解決策は簡単です。代わりにあなたがCountryLangs(「s」を注意してください)

を保存したいList<CountryLang>を格納する私はここで解決策の簡単な例をやった:

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は、あなたの前のオブジェクトの反転です。これは、言語のリストを格納するオブジェクトです。あなたの言語を格納するオブジェクトのリストの代わりに。

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; 
    } 
} 

このコンバータは、文字列のリストを受け取り、1つの列に格納されるカンマ区切り文字列に変換します。変換するSQLite dbから文字列をフェッチすると、コンマでリストが分割され、CountryLangsが生成されます。

これらの変更を行った後、RoomDatabaseのバージョンを更新するようにしてください。残りの設定は正しいです。あなたの部屋の永続性の仕事の残りの部分でハッピーハンティング。

+0

Jack、Enumのリストを保存する方法は知っていますか? – murt

+0

私は一見を持って、他の人が簡単に質問+回答を見つけることができるように質問として投稿します。コミュニティを手助けしてください:) –

+1

Enumのリストを使用https://stackoverflow.com/q/44698415/2163045 – murt

2

日付フィールドを追加しようとしているときに同じエラーが発生しました:「このフィールドをデータベースに保存する方法を見つけることができませんでした」。コンバータクラスを追加して@TypeConvertersアノテーションをフィールドに追加しなければなりませんでした。

例:

WordEntity.java

import android.arch.persistence.room.TypeConverters; 

    @Entity 
    public class WordEntity { 

     @PrimaryKey(autoGenerate = true) 
     public int id; 
     private String name; 
     @TypeConverters(DateConverter.class) 
     private Date createDate; 

DateConverter.java:

import android.arch.persistence.room.TypeConverter; 

import java.util.Date; 

public class DateConverter { 

    @TypeConverter 
    public static Date toDate(Long timestamp) { 
     return timestamp == null ? null : new Date(timestamp); 
    } 

    @TypeConverter 
    public static Long toTimestamp(Date date) { 
     return date == null ? null : date.getTime(); 
    } 
} 
1

@TypeConverterあなたの代わりにArrayListを使用する必要がありますので、あなたはドンので、Listクラスを認識しません」保持したいリストの追加ラッパーが必要です。

0

また、Stringにあなたのリストに変換され、このTypeconvertorを作成する必要が

@TypeConverter 
public List<CountryLang> toCountryLangList(String countryLangString) { 
    if (countryLangString == null) { 
     return (null); 
    } 
    Gson gson = new Gson(); 
    Type type = new TypeToken<List<CountryLang>>() { 
    }.getType(); 
    List<CountryLang> countryLangList = gson.fromJson(countryLangString, type); 
    return countryLangList; 
} 

そして、より多くの情報のために、あなたはまた、私の別のanswerを確認することができます。

関連する問題