Intent 传递 OrmLite 对象 ID 值丢失?

Intent Pass OrmLite Object ID Value Missing?

import android.os.Parcel;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "t_notice")
public class NoticeBody implements android.os.Parcelable {
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField
    private String userId;
    @DatabaseField
    private boolean isRead = false;
    @DatabaseField
    private String content;
    @DatabaseField
    private String title;
    @DatabaseField
    private String time;
    @DatabaseField
    private String noticeId;
    @DatabaseField
    private String lastUpdateTime;

    public String getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(String lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    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 getNoticeId() {
        return noticeId;
    }

    public void setNoticeId(String noticeId) {
        this.noticeId = noticeId;
    }

    public NoticeBody() {
    }
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.userId);
        dest.writeByte(this.isRead ? (byte) 1 : (byte) 0);
        dest.writeString(this.content);
        dest.writeString(this.title);
        dest.writeString(this.time);
        dest.writeString(this.noticeId);
        dest.writeString(this.lastUpdateTime);
    }

    protected NoticeBody(Parcel in) {
        this.userId = in.readString();
        this.isRead = in.readByte() != 0;
        this.content = in.readString();
        this.title = in.readString();
        this.time = in.readString();
        this.noticeId = in.readString();
        this.lastUpdateTime = in.readString();
    }

    public static final Creator<NoticeBody> CREATOR = new Creator<NoticeBody>() {
        @Override
        public NoticeBody createFromParcel(Parcel source) {
            return new NoticeBody(source);
        }

        @Override
        public NoticeBody[] newArray(int size) {
            return new NoticeBody[size];
        }
    };

    @Override
    public String toString() {
        return "NoticeBody{" + "id=" + id + ", userId='" + userId + '\'' + ", isRead=" + isRead + ", content='" + content + '\'' + ", title='" + title + '\'' + ", time='" + time + '\'' + ", noticeId='" + noticeId + '\'' + ", lastUpdateTime='" + lastUpdateTime + '\'' + '}';
    }

}

点击消息列表中的消息跳转到详情页面

  Intent intent = new Intent(NoticeActivity.this, NoticeDetailActivity.class);
                    intent.putExtra("noticInfo", info);
                    HPLog.d(HPLog.SSX_TAG, "put:" + info.toString());
                    NoticeActivity.this.startActivity(intent);

详情activity接收消息已通过

    NoticeBody notice = getIntent().getParcelableExtra("noticInfo");
    if (notice != null) {
        HPLog.d(HPLog.SSX_TAG, "get:" + notice.toString());
}

但是结果是

ssx:(NoticeActivity.java:174)→put:NoticeBody{id=1, userId='19645238', isRead=true, content='message', title='title' , time='20170510175931', noticeId='375905', lastUpdateTime='20170510175931'}

ssx:(NoticeDetailActivity.java:42)→get:NoticeBody{id=0, userId='19645238', isRead=true, content='content', title='title' , time='20170510175931', noticeId='375905', lastUpdateTime='20170510175931'}

你的id属性没有读写from/to包裹对象

你应该:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.id); // <-- add this line
    dest.writeString(this.userId);

protected NoticeBody(Parcel in) {
    this.id = in.readInt();  // <-- and add this line
    this.userId = in.readString();

ID属性没有GET方法时会出现这种情况,导致对象每次都是默认值0

import android.os.Parcel;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 *
 * @email:emotiona_xiaoshi@126.com
 * @describe:Describe the function  of the current class
 */
@DatabaseTable(tableName = "t_notice")
public class NoticeBody implements android.os.Parcelable {
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField
    private String userId;
    @DatabaseField
    private boolean isRead = false;
    @DatabaseField
    private String content;
    @DatabaseField
    private String title;
    @DatabaseField
    private String time;
    @DatabaseField
    private String noticeId;
    @DatabaseField
    private String lastUpdateTime;

    public int getId() {//add this method
        return id;
    }


    public String getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(String lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    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 getNoticeId() {
        return noticeId;
    }

    public void setNoticeId(String noticeId) {
        this.noticeId = noticeId;
    }

    public NoticeBody() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        NoticeBody that = (NoticeBody) o;

        if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;
        return noticeId != null ? noticeId.equals(that.noticeId) : that.noticeId == null;

    }

    @Override
    public int hashCode() {
        int result = userId != null ? userId.hashCode() : 0;
        result = 31 * result + (noticeId != null ? noticeId.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "NoticeBody{" + "id=" + id + ", userId='" + userId + '\'' + ", isRead=" + isRead + ", content='" + content + '\'' + ", title='" + title + '\'' + ", time='" + time + '\'' + ", noticeId='" + noticeId + '\'' + ", lastUpdateTime='" + lastUpdateTime + '\'' + '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.userId);
        dest.writeByte(this.isRead ? (byte) 1 : (byte) 0);
        dest.writeString(this.content);
        dest.writeString(this.title);
        dest.writeString(this.time);
        dest.writeString(this.noticeId);
        dest.writeString(this.lastUpdateTime);
    }

    protected NoticeBody(Parcel in) {
        this.id = in.readInt();
        this.userId = in.readString();
        this.isRead = in.readByte() != 0;
        this.content = in.readString();
        this.title = in.readString();
        this.time = in.readString();
        this.noticeId = in.readString();
        this.lastUpdateTime = in.readString();
    }

    public static final Creator<NoticeBody> CREATOR = new Creator<NoticeBody>() {
        @Override
        public NoticeBody createFromParcel(Parcel source) {
            return new NoticeBody(source);
        }

        @Override
        public NoticeBody[] newArray(int size) {
            return new NoticeBody[size];
        }
    };
}