gpt4 book ai didi

android - 使用 Room 在 Insert 命令上传递列表复制 Android 中的值

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:31 26 4
gpt4 key购买 nike

我正在传递国家/地区列表,以使用 Room 将其保存在数据库中。它保存但复制值和 OnConflict Replace 策略不起作用。

AppDatabase.getAppDatabase(getApplicationContext()).countryDao().insertAllList(countryList);

即使我通过了以下替换策略,列表值也会被复制。

@Insert(onConflict = OnConflictStrategy.REPLACE)

这是CountryDao

@Dao
public interface CountryDao {

@Query("SELECT * FROM country")
List<Country> getAllCountries();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAllList(List<Country> countries);

国家对象:

 @Entity(tableName = "country")
public class Country {
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@PrimaryKey(autoGenerate = true)
private int id;
private long countryId;
private String countryName;

public long getCountryId() {
return countryId;
}

public void setCountryId(long countryId) {
this.countryId = countryId;
}

public String getCountryName() {
return countryName;
}

public void setCountryName(String countryName) {
this.countryName = countryName;
}
}

最佳答案

您必须为每个国家/地区设置不同的 id 并手动分配它,否则每个元素都会有 id=0 并覆盖另一个。

尝试不自动生成 key

@PrimaryKey(autoGenerate = false)
private int id;

关于android - 使用 Room 在 Insert 命令上传递列表复制 Android 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45691276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com