如何使用Room Android插入空字段 [英] How to insert null field using Room Android

查看:67
本文介绍了如何使用Room Android插入空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试并了解Android的Room Persistence库.到目前为止,我已经了解了如何插入和查询它们的方法,但是现在我试图拥有2个构造函数,以便在不想插入字段时不插入它们.

I'm trying to play around and understand the Room persistence library of Android. So far I got how to insert things and query them, but now I'm trying to have 2 constructors in order to not insert a field if I dont want to.

类似这样的东西:

    public ImageData(int id, String name, String title, String description, int locationId) {

 .....

      public ImageData(int id, String name, String title, String description) {

在某些情况下,我没有位置,因此我不想在该int位置列中插入任何内容.这是实现它的正确方法(显然不是因为我收到错误)吗?

In some cases I won't have a location and therefore I don't want to insert anything in that int Location column. Is this the right way to achieve it(apparently not because I get errors)?

Error:(38, 12) error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted constructors with @Ignore.

如果您需要更多信息,请告诉我.

Please let me know if you need more information.

@Entity(primaryKeys = {"id", "name"},
        foreignKeys = @ForeignKey(entity = Location.class,
        parentColumns = "id",
        childColumns = "location_id",
        onDelete=CASCADE))
public class ImageData {

    @NonNull
    public int id;
    @NonNull
    public String name;

    public String title;
    public String description;
    public String time;

    @ColumnInfo(name = "location_id")
    @Nullable
    public int locationId;


    public ImageData(int id, String name, String title, String description, int locationId) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.description = description;
        this.locationId = locationId;
    }

    public ImageData(int id, String name, String title, String description) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.description = description;
    }


    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getName() {
        return title;
    }

    public void setName(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getLocationId() {
        return locationId;
    }

    public void setLocationId(int locationId) {
        this.locationId = locationId;
    }

}

@Entity
public class Location {

        @PrimaryKey(autoGenerate = true)
        public int id;

        public double latitude;
        public double longitude;

        public Location(double latitude, double longitude) {
                this.latitude= latitude;
                this.longitude= longitude;
        }

        public int getId() {
                return this.id;
        }
}

推荐答案

您应该进行以下更改,可以使用@ignore注释.

You should do the following changes, you can use @ignore annotation.

  public ImageData(int id, String name, String title, String description, int locationId) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.description = description;
        this.locationId = locationId;
    }

    @Ignore
    public ImageData(int id, String name, String title, String description) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.description = description;
    }

这篇关于如何使用Room Android插入空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆