在我的Android應用程序,我在火力數據庫中插入一個VideoEntity對象是這樣的:如何避免重複的數據在火力地堡數據庫
@Override
protected void onCreate(Bundle savedInstanceState){
Log.d(TAG, " onCreate(Bundle) - Ini ");
super.onCreate(savedInstanceState);
database = FirebaseDatabase.getInstance();
String videoId = "";
for(VideoEntity video: videosList) {
videoId = video.getId();
DatabaseReference mRef = database.getReference().child("Videos").push();
mRef.setValue(video);
}
這個VideoEntity類:
package com.app.entities;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
public class VideoEntity implements Parcelable{
private String id;
private String title;
private String description;
private String thumbnailURL;
private String category;
public VideoEntity() {
}
public VideoEntity(Parcel in) {
id = in.readString();
title = in.readString();
description = in.readString();
thumbnailURL = in.readString();
category = in.readString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnail) {
this.thumbnailURL = thumbnail;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {return category;}
public void setCategory(String category) { this.category = category;}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(title);
dest.writeString(description);
dest.writeString(thumbnailURL);
dest.writeString(category);
}
public static final Creator<VideoEntity> CREATOR = new Creator<VideoEntity>() {
@Override
public VideoEntity createFromParcel(Parcel in) {
return new VideoEntity(in);
}
@Override
public VideoEntity[] newArray(int size) {
return new VideoEntity[size];
}
};
}
的問題是,每次我開始這個活動時,都會在數據庫中插入相同的對象,所以我在數據庫中有重複的數據。
有沒有辦法避免這種情況?
請解釋更多關於你想要做什麼,你期望發生什麼,以及發生了什麼不同。 「插入了相同的數據?」你什麼意思?這個「videolist」數組/對象來自哪裏? –
我不認爲這是相關的知道videolist從哪裏來,但我編輯我的問題。 – rainman