Android房间TypeConveter无法正常工作

时间:2017-10-03 16:43:53

标签: android android-room

我的entity(table)名为moviesattribute(column)名为subscriberEmails

@Entity(tableName = "movies")
public class DBMovies {
    ....
    private ArrayList<String> subscriberEmails;
    ....
}

我使用以下TypeConverter来保存和检索数据库中的ArrayList

我可以使用以下Query

保存实体
@Insert(onConflict = OnConflictStrategy.REPLACE)
void addList(DBMovies dBMovies );

但是,当我检索subscriberEmails而不是收到ArrayList封电子邮件时,我会收到ArrayList,如图所示。

enter image description here

因此,客房无法使用以下TypeConverter将字符串转换回ArrayList

@TypeConverter
public static String arrayListToString(ArrayList<String> listToBeConverted) {
    return gson.toJson(listToBeConverted);
}

@TypeConverter
public static ArrayList<String> stringToArrayList(String stringToBeConverted) {
    Type type = new TypeToken<ArrayList<String>>() {}.getType();
    return gson.fromJson(stringToBeConverted, type);
}

TypeConverter已在AppDatabase

中注册
@Database(entities = {DBMovies.class}, version = 1, exportSchema = false)
@android.arch.persistence.room.TypeConverters({TypeConverters.class})
public abstract class AppDatabase extends RoomDatabase{
...
}

0 个答案:

没有答案