ActiveAndroid仅保存唯一数据

时间:2016-12-09 08:21:11

标签: android activeandroid

我学会在我的Android应用程序中使用ActiveAndroid,有人能告诉我如何仅保存具有唯一ID的数据吗?在我的模型中,我将id设置为非主键列。这是我的模型类:

package com.project.echo.contactmanagement.data;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;
import com.google.gson.annotations.Expose;

import java.util.List;


@Table(name="Contacts" , id = "idColumn")
public class Post extends Model {

@Expose
@Column(name = "ProfilePic")
public String profilePic;

@Expose
@Column(name = "id")
public Integer id;

@Expose
@Column(name = "FirstName")
public String firstName;

@Expose
@Column(name = "LastName")
public String lastName;

public Post()
{
    super();
}

public Post(String profilePic, Integer id, String firstName, String lastName)
{
    this.profilePic=profilePic;
    this.id=id;
    this.firstName=firstName;
    this.lastName=lastName;
}

public String getProfilePic() {
    return profilePic;
}


/*public Integer getId() {
    return id;
}*/


public String getFirstName() {
    return firstName;
}


public String getLastName() {
    return lastName;
}

public static List selectAll() {
    return new Select().from(Post.class).execute();
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以通过使id列唯一来插入唯一记录。

@Column(name = "id", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
private Integer id;

注意:在ActiveAndroid中, com.activeandroid.Model 类已经有一个名为 id ,所以你可能需要重命名id列才能达到目的。