如何使用最近的日期对RealmResults进行排序?

时间:2018-01-07 15:37:45

标签: android sql realm realm-list

我在RealmResults中有大约200行,需要使用最近的日期对列表进行排序,我还需要选择最后20个元素然后每20个元素

 RealmResults<Transactions> transactions
                    = realm.where(Transactions.class)
                    .sort("date",Sort.DESCENDING).between(
                            "id", start, start + limit).
                    findAll();




public class Transactions extends RealmObject implements Serializable {
@PrimaryKey
private long id;

public long getId() {
    return id;
}

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

private String amount = "";

public String getTransactionStatus() {
    return transactionStatus;
}

public void setTransactionStatus(String transactionStatus) {
    this.transactionStatus = transactionStatus;
}



// 1 complete,0 pay with cash,2 fail status
private String transactionStatus = "";


private String signature = "";

public String getSignature() {
    return signature;
}

public void setSignature(String signature) {
    this.signature = signature;
}

private String dateTime = "";

public String getDateTime() {
    return dateTime;
}

public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
}

private String paymentType = "";

public String getPaymentType() {
    return paymentType;
}

public void setPaymentType(String paymentType) {
    this.paymentType = paymentType;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}


public String getLogo() {
    return logo;
}

public void setLogo(String logo) {
    this.logo = logo;
}

public String getCompanyId() {
    return companyId;
}

public void setCompanyId(String companyId) {
    this.companyId = companyId;
}

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getCompanyIdentification() {
    return companyIdentification;
}

public void setCompanyIdentification(String companyIdentification) {
    this.companyIdentification = companyIdentification;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getSerialNumber() {
    return serialNumber;
}

public void setSerialNumber(String serialNumber) {
    this.serialNumber = serialNumber;
}

public String getTerminalId() {
    return terminalId;
}

public void setTerminalId(String terminalId) {
    this.terminalId = terminalId;
}

public String getPsamModel() {
    return psamModel;
}

public void setPsamModel(String psamModel) {
    this.psamModel = psamModel;
}

public String getAdditionalText() {
    return additionalText;
}

public void setAdditionalText(String additionalText) {
    this.additionalText = additionalText;
}


private String logo = "";

private String companyId = "";

private String company = "";

private String companyIdentification = "";

private String address = "";

private String serialNumber = "";

private String terminalId = "";


private String psamModel = "";

private String additionalText = "";

}

这是我的查询。没有,我可以得到所有项目,但我也想在两者之间使用 如何重写我的代码以选择最后20个元素,并在第二个调用我的函数,另外20 + 20个元素 感谢

1 个答案:

答案 0 :(得分:0)

RealmResults是延迟加载的,因此您加载实际访问的内容,因此List.subList应该为您提供所需内容:

// Page through the results. Use Sort.ASCENDING if you want the reverse.
RealmResults<Transaction> results = getTransactions();
int pageSize = 20;
int start = 0;
int end = pageSize;
int size = results.size();
while (start < size - 1) {
  end = Math.min(end, size);
  List<Transaction> page = results.subList(start, end);
  handlePage(page);
  start += page;
  end += page

}