在我的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