使用领域检索单个人的聊天记录

时间:2017-03-19 15:40:37

标签: java android realm realm-mobile-platform

我正在尝试制作聊天应用,我使用领域将聊天记录存储在数据库中。我将我的用户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类

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;
    }
}

1 个答案:

答案 0 :(得分:3)

你试过吗

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();