Android 房间持久库 - 如何插入具有 List 对象字段的类

标签 android android-room

Android room persistent library如何将整个 Model 对象插入到本身具有另一个列表的表中。

让我告诉你我的意思:

@Entity(tableName = TABLE_NAME)
public class CountryModel {

    public static final String TABLE_NAME = "Countries";

    @PrimaryKey
    private int idCountry;

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

    /** 
        here i am providing a list of coutry information how to insert 
        this into db along with CountryModel at same time 
    **/
    public List<CountryLang> getCountryLang() {
        return countryLang;
    }

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

我的 DAO 如下所示:

@Dao
public interface CountriesDao{

    @Query("SELECT * FROM " + CountryModel.TABLE_NAME +" WHERE isoCode =:iso_code LIMIT 1")
    LiveData<List<CountryModel>> getCountry(String iso_code);

    @Query("SELECT * FROM " + CountryModel.TABLE_NAME )
    LiveData<List<CountryModel>> getAllCountriesInfo();

    @Insert(onConflict = REPLACE)
    Long[] addCountries(List<CountryModel> countryModel);

    @Delete
    void deleteCountry(CountryModel... countryModel);

    @Update(onConflict = REPLACE)
    void updateEvent(CountryModel... countryModel);
}

当我调用 database.CountriesDao().addCountries(countryModel); 我得到以下房间数据库编译错误: 错误:(58, 31) 错误:无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。

应该有另一个名为 CountryLang 的表吗?如果是这样,如何告诉空间在插入语句中连接它们?

CountryLang 对象本身如下所示:

public class CountryLang {


    private int idCountry;

    private int idLang;

    private String name;

    public int getIdCountry() {
        return idCountry;
    }

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

    public int getIdLang() {
        return idLang;
    }

    public void setIdLang(int idLang) {
        this.idLang = idLang;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

响应如下所示:

"country_lang": [
      {
        "id_country": 2,
        "id_lang": 1,
        "name": "Austria"
      }
    ]

对于每个国家/地区,因此这里不会超过一项。我很乐意为 country_lang 列表中的一项设计它。所以我可以为 country_lang 制作一个表格,然后将其链接到 CountryModel。但如何?我可以使用外键吗?我希望我不必使用平面文件。所以你说我必须将它存储为 json ?是否建议不要临时使用房间?改用什么?

最佳答案

您可以使用 TypeConverterGSON 轻松插入带有列表对象字段的类,

public class DataConverter {

    @TypeConverter
    public String fromCountryLangList(List<CountryLang> countryLang) {
        if (countryLang == null) {
            return (null);
        }
        Gson gson = new Gson();
        Type type = new TypeToken<List<CountryLang>>() {}.getType();
        String json = gson.toJson(countryLang, type);
        return json;
    }

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

接下来,将@TypeConverters 注解添加到 AppDatabase 类中

    @Database(entities = {CountryModel.class}, version = 1)
    @TypeConverters({DataConverter.class})
    public abstract class AppDatabase extends RoomDatabase {
      public abstract CountriesDao countriesDao();
    }

有关房间中的TypeConverters的更多信息,请查看我们的博客hereofficial docs .

关于Android 房间持久库 - 如何插入具有 List 对象字段的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580702/

相关文章:

java - 如何在 Android 上实现 3 种或更多颜色之间的淡入淡出

android - 使用 Room 时如何手动调用 CREATE TABLE?

android - WorkManager 的 ForceStopRunnable 中的 SQLiteException

database - 将图像保存在房间数据库中

java - 如何在Fragment之间共享公共(public)资源?

不同设备的Android重复注册ID

android - 如何更改 DatePicker Header Android 中的文本颜色

php - Android UTF-8 西里尔文请求

android - Firestore 作为离线持久性机制有多可靠?

android - 在 gradle 中添加 kapt androidx room 会导致数据绑定(bind)错误