简体   繁体   中英

get next and previous rows of a given id with ormlite and android

我想使用ormlite和android获取给定ID的下一行和上一行,表格中的ID不会自动递增

You can do this: Get all rows:

List<MyDataObject> list = myDao.queryForAll();

Search the index of your object in the list:

int myIndex;
for (MyDataObject obj: list) {
    if (obj.getId().equals("Myid")) {
        myIndex = list.indexOf(obj)
    }
}

Get your wanted items:

MyDataObject previousObject = list.get(myIndex+1);
MyDataObject nextObject = list.get(myIndex-1);

This is one approach. Another way is get only a list of your ids and then search your wanted ids (next and previous ids) and finally make a query by id.

I want to get next and previous rows of a given id with ormlite and android the ids in the table is not auto incremented

I'm not 100% sure I understand the question but you can certainly select for items that are greater-than your id, ordered by the id, and limit 1. That would give the next (greater) row. Using less-than would give the previous row.

Something like:

QueryBuilder<Foo> qb = fooDao.queryBuilder();
// we want the items greater than this id
qb.where().gt("id", id); // use lt for previous
// we only want the first one
qb.limit(1);
// order by the id
qb.orderBy("id");
Foo previousFoo = qb.queryForFirst();

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM