2

Please help, i'm kinda newbie when it comes to android app with a database.

In Greendao documentation there's a point of having this code:

List users = usersDao.queryBuilder().orderDesc(Properties.Id).list();

But somehow, it is not documented well on how to exactly get the values of rows from that query builder? or is there even a point where I could only get the first row, or last row from the database?

Thanks in advance for someone who will help.

Ari
  • 3,101
  • 2
  • 27
  • 49
user2641085
  • 101
  • 3
  • 8

2 Answers2

4

Entity class are similar to all other classes. So greendao generates properties for all rows and you can access them as you access all others properties form "normal" classes.

To access Id property of UserEntity you can use getter user.getId().

From this query you are getting list of users. You can access it as you are accessing any other list in Java.

To get first user from database you can use code similar to:

List<User> users = usersDao.queryBuilder().orderDesc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);

To get last user I suggest to use similar query with reversed order:

List<User> users = usersDao.queryBuilder().orderAsc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);

If your query returns only one unique entity you can use unique method:

User user = usersDao.queryBuilder().where(...).unique();

You don't need to build these queries all the time. You can build it once, and then reuse it:

private Query<User> mSomeUserQuery;
...
// initialization
mSomeUserQuery = usersDao.queryBuilder().where(...).build();
// usage
User user = mSomeUserQuery.forCurrentThread().unique();
Ari
  • 3,101
  • 2
  • 27
  • 49
  • Thanks for the answer! :D I'm sorry i can't vote up yet because i don't have enough reputation for vote up! thanks again! – user2641085 Jul 02 '14 at 09:27
2

Greendao lists are not different to other lists in Java. You just have to google how to iterate through lists. Result: Ways to iterate over a List in java?

But here is the answer with the example to get the name value from an user:

for(User user : users){
     String name = user.getName();
 }
Community
  • 1
  • 1
TheOnlyJakobob
  • 541
  • 3
  • 14