领域选择从0到20的行

时间:2017-12-08 13:01:09

标签: android sql sqlite android-recyclerview realm

我的数据库中有100个元素。我想首先选择前20个元素并在recyclerview中显示它。我的目标是在recyclelerView加载更多事件中将此值增加20。我知道如何在recyclelerView中捕获更多事件,但我不知道如何在表中加载select元素加载更多动作。

我编写了一个可以选择所有元素的函数:

    public void getTransactions(int start,int end)
{
    try {
        CoreApplication.realm = Realm.getDefaultInstance();
        CoreApplication.realm.executeTransaction(realm -> {
            final RealmResults<Transactions> transactions =
                    realm.where(Transactions.class).findAll(); 
            if (transactions != null && transactions.size() > 0) {
                for (int i = 0; i < transactions.size(); i++) {
                    Log.e("terminalTables", transactions.get(i).getAdditionalText());
                    Log.e("terminalTables", transactions.get(i).getId()+"");

                }
            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (CoreApplication.realm!= null) {
            CoreApplication.realm.close();
        }
    }

}

如何重写我的代码以在RealmResults中添加两个选项? (开始和结束)

1 个答案:

答案 0 :(得分:0)

这是一个解决方案

    public void getTransactions(int start, int end) {
    try {
        CoreApplication.realm = Realm.getDefaultInstance();
        CoreApplication.realm.executeTransaction(realm -> {
            final OrderedRealmCollection<Transactions> transactions =
                    realm.where(Transactions.class).between("id", start, start+end).findAll().createSnapshot();
            if (transactions != null && transactions.size() > 0) {
                for (int i = 0; i < transactions.size(); i++) {
                    Log.e("terminalTables", transactions.get(i).getAdditionalText());
                    Log.e("terminalTables", transactions.get(i).getId() + "");

                }
            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (CoreApplication.realm != null) {
            CoreApplication.realm.close();
        }
    }

}