我正在开发一个聊天应用程序,并将User模型类成功保存到领域中,但是现在我想将消息保存到领域中,以便当用户启动应用程序时能够查看以前的聊天记录和消息。
在SavedMessages模型类下方
@RealmClass
公共类SaveMessage扩展了RealmObject {
private int mId;
private String mUsername;
private String mContent;
private Date mCreatedAt;
private boolean mRightMessage;
private String mPictureString;
private String mType;
public SaveMessage(int id, String username, String content, Date createdAt, boolean isRightMessage) {
mId = id;
mUsername = username;
mContent = content;
mCreatedAt = createdAt;
mRightMessage = isRightMessage;
}
public SaveMessage() {
}
public int getId() {
return mId;
}
public String getUsername() {
return mUsername;
}
public String getContent() {
return mContent;
}
public Date getDate() {
return mCreatedAt;
}
public void setDate(Date createdAt) {
mCreatedAt = createdAt;
}
public boolean ismRightMessage() {
return mRightMessage;
}
public int getmId() {
return mId;
}
public void setmId(int mId) {
this.mId = mId;
}
public String getmUsername() {
return mUsername;
}
public void setmUsername(String mUsername) {
this.mUsername = mUsername;
}
public String getmContent() {
return mContent;
}
public void setmContent(String mContent) {
this.mContent = mContent;
}
public boolean ismRightMessage(String isRightMessage) {
return mRightMessage;
}
public void setmRightMessage(boolean mRightMessage) {
this.mRightMessage = mRightMessage;
}
public String getPictureString() {
return mPictureString;
}
public void setPictureString(String pictureString) {
mPictureString = pictureString;
}
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
}
User.java下面
@RealmClass
public class User extends RealmObject implements IChatUser {
Integer id;
String name;
@Ignore
Bitmap icon;
public User() {
}
public User(int id, String name, Bitmap icon) {
this.id = id;
this.name = name;
this.icon = icon;
}
@Override
public String getId() {
return this.id.toString();
}
@Override
public String getName() {
return this.name;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public Bitmap getIcon() {
return this.icon;
}
@Override
public void setIcon(Bitmap icon) {
this.icon = icon;
}
}
在我实现了领域数据库的领域实现下面
Realm.init(this);
realm = Realm.getDefaultInstance();
realm.beginTransaction();
User userModel = realm.createObject(User.class);
Integer userId = Integer.valueOf("0");
String userName = "Michael";
userModel.setId(userId);
userModel.setName(userName);
//userModel.setIcon("icon");
Integer userId1 = Integer.valueOf("1");
String userName1 = "Lily";
userModel.setId(userId1);
userModel.setName(userName1);
realm.commitTransaction();
RealmResults<User> userModels = realm.where(User.class).findAll();
for (User user : userModels) {
Integer getId = Integer.valueOf(user.getId());
String getName = user.getName();
}
SaveMessage saveModel = realm.createObject(SaveMessage.class);
saveModel.setmId(Integer.parseInt("id"));
saveModel.setmUsername("userName");
saveModel.setmContent("content");
saveModel.ismRightMessage("isRightMessage");
RealmResults<SaveMessage> savesModels = realm.where(SaveMessage.class).findAll();
for (SaveMessage saveMessage : savesModels) {
Integer getId = saveMessage.getId();
String getName = saveMessage.getUsername();
String getContent = saveMessage.getContent();
String rightMessage = String.valueOf(saveMessage.ismRightMessage());
realm.commitTransaction();
}
//Load saved messages
loadMessages(realm);