使用 realm 检索单个人的聊天记录
Retreive the chat history of a single person using realm
我正在尝试制作聊天应用程序,我在其中使用领域将聊天记录存储在数据库中。我将我的用户 ID 和其他人的 ID 存储到数据库中。如何获取我和其他人的聊天记录数据?这是我正在使用的查询,但我只收到我的聊天消息,没有收到其他消息。
chathistorylist = chatrealm.where(Chat_history_pojo.class)
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.findAll();
下面是我存储我发送给其他人的消息的数据
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setReceiverid(getIntent().getStringExtra("rid"));
chatupdatepojo.setMessage(sendmessgae.getText().toString());
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
sendmessgae.setText("");
这就是我将其他人的消息存储到数据库中的方式
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(getIntent().getStringExtra("rid"));
chatupdatepojo.setReceiverid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setMessage(intent.getStringExtra("msg"));
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
这是 POJO Class
public class Chat_history_pojo extends RealmObject{
String senderid;
String receiverid;
String message;
String type;
String sender_profile;
String reciever_profile;
public String getSender_profile() {
return sender_profile;
}
public void setSender_profile(String sender_profile) {
this.sender_profile = sender_profile;
}
public String getReciever_profile() {
return reciever_profile;
}
public void setReciever_profile(String reciever_profile) {
this.reciever_profile = reciever_profile;
}
public String getSenderid() {
return senderid;
}
public void setSenderid(String senderid) {
this.senderid = senderid;
}
public String getReceiverid() {
return receiverid;
}
public void setReceiverid(String receiverid) {
this.receiverid = receiverid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
你试过了吗
chathistorylist = chatrealm.where(Chat_history_pojo.class)
.beginGroup()
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.endGroup()
.or()
.beginGroup()
.equalTo("receiverid", Session.getUserID(getApplicationContext()))
.equalTo("senderid", getIntent().getStringExtra("rid"))
.endGroup()
.findAll();
?
我正在尝试制作聊天应用程序,我在其中使用领域将聊天记录存储在数据库中。我将我的用户 ID 和其他人的 ID 存储到数据库中。如何获取我和其他人的聊天记录数据?这是我正在使用的查询,但我只收到我的聊天消息,没有收到其他消息。
chathistorylist = chatrealm.where(Chat_history_pojo.class)
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.findAll();
下面是我存储我发送给其他人的消息的数据
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setReceiverid(getIntent().getStringExtra("rid"));
chatupdatepojo.setMessage(sendmessgae.getText().toString());
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
sendmessgae.setText("");
这就是我将其他人的消息存储到数据库中的方式
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(getIntent().getStringExtra("rid"));
chatupdatepojo.setReceiverid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setMessage(intent.getStringExtra("msg"));
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
这是 POJO Class
public class Chat_history_pojo extends RealmObject{
String senderid;
String receiverid;
String message;
String type;
String sender_profile;
String reciever_profile;
public String getSender_profile() {
return sender_profile;
}
public void setSender_profile(String sender_profile) {
this.sender_profile = sender_profile;
}
public String getReciever_profile() {
return reciever_profile;
}
public void setReciever_profile(String reciever_profile) {
this.reciever_profile = reciever_profile;
}
public String getSenderid() {
return senderid;
}
public void setSenderid(String senderid) {
this.senderid = senderid;
}
public String getReceiverid() {
return receiverid;
}
public void setReceiverid(String receiverid) {
this.receiverid = receiverid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
你试过了吗
chathistorylist = chatrealm.where(Chat_history_pojo.class)
.beginGroup()
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.endGroup()
.or()
.beginGroup()
.equalTo("receiverid", Session.getUserID(getApplicationContext()))
.equalTo("senderid", getIntent().getStringExtra("rid"))
.endGroup()
.findAll();
?