簡體   English   中英

如何避免在Android Room中重復

[英]How to avoid duplicating in Android Room

我想創建一個存儲踏板車名稱的應用程序,但是當我搜索名稱時,我得到的是重復項,我正在使用Room for Android,這是我的代碼。 有人請幫助我,謝謝。

POJO課程

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "battery")
private String battery;

@ColumnInfo(name = "code")
private String birdCode;

@ColumnInfo(name = "latitude")
private double lat;

@ColumnInfo(name = "longitude")
private double lng;

道接口

@Query("SELECT * FROM BirdsRoom" )
List<BirdsRoom> getCachedBirds();

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertBirds(BirdsRoom... birdsRoom);

我如何插入數據

Birds birds = response.body().getBirdsList().get(i);
BirdLocation birdLocation = birds.birdLocation;
BirdLocation location = birds.birdLocation;
BirdsRoom birdsRoom = new BirdsRoom(birds.getCode(),birds.getBatteryLevel(), 
birdLocation.getLat(), birdLocation.getLng());
birdsDatabase.getUserDao().insertBirds(birdsRoom);

如果您想在BirdsRoom中使用唯一的電池和代碼來設置注釋,如下所示

@Entity(indices = {@Index(value = {"battery", "code"}, unique = true)})
public class BirdsRoom {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo(name = "battery")
private String battery;

@ColumnInfo(name = "code")
private String birdCode;

@ColumnInfo(name = "latitude")
private double lat;

@ColumnInfo(name = "longitude")
private double lng;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM