How to iterate through greendao list? - android

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.

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

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

Related

Get db from current user firestore query kotlin android

whats up?
I have an app that displays a list of items on Firestore using Kotlin and RecyclerView (from FirebaseUI api).
The DB is structured like this:
Users/userID/document_month_year
I need to query the data from the current user.
Each user has his own document_month_year document.
I read a lot of posts here, but each one tell one thing.. thereĀ“s no consense and nothing seems to work.
This query just sends me all documents from all users, how can I fix this?
private val queryFilteredByPaidStatus = db.collectionGroup(collectionName).whereEqualTo("users", userId)
Like this is an important question, here is the awnser that I
private val queryTest = db.document("users/"+userId).collection(collectionName)
fun getData() : FirestoreRecyclerOptions<Expense> {
return FirestoreRecyclerOptions
.Builder<Expense>()
.setQuery(queryTest, Expense::class.java)
.build()
}
Create a separate collection for documents to be read i.e month_year and for each document, add a field inside it which tells you the uid of the authenticated user, to which the document belongs to. Now you can query the collection like:
firestoreDB.collections("month_year").whereEqualTo("uid",auth.currentUser.uid)

Android's Firestore to join 2 collections into a recycler view

I have a users collection with uId, name, photo
I have a visits collection with uId, userId, location
I have a recyclerview in which I want to show the location with the user name and photo
Can I use the reference field type? If so, how will Firestore know to link visits.userId == users.uId ?
Maybe I first need to query all the visits and then query the relevant user but 2 things:
It means querying a lot of times.
I didn't understand how to collect the joined collection into the adapter, which is based on one query?
Please advice
Thanks
current code
visitsList = db.collection("visitsList");
Query query = visitsList.whereEqualTo("userId",prefs.getString("id","")).orderBy("visitDate", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<AVisit> options = new FirestoreRecyclerOptions.Builder<AVisit>().setQuery(query, AVisit.class).build();
adapter = new VisitsListAdapter(options, VisitsListActivity.this);
RecyclerView rv = findViewById(R.id.rvVisitsList);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(adapter);
The code is a simple query from the collection, not sure how to get the name and photo from the userId field in that collection.
Can I use the reference field type?
Yes, you can use a reference field.
If so, how will Firestore know to link visits.userId == users.uId ?
Firestore results always comes from a single collection (at the moment). It does not automatically join the document from the users collection when you're reading from the visits collection. You will have to do this yourself.
That indeed means you'll be executing multiple reads, but it's often not nearly as slow as you may think. See Google Firestore - how to get document by multiple ids in one round trip?
Update: To show data from the user profile in a list of visits, there are two main options:
load the additional user document in populateView or with a custom parseSnapshot implementation.
duplicate the relevant user data in the visits collection (which is quite normal in NoSQL databases). Also see Alex' answer here: indexed query with FirestoreRecyclerAdapter.

Return last inserted record with greenDAO

I am developing an android App with greenDAO, I need to insert an order-detail but I dont't find a practical way to do it, I don't know if there is a method that returns the created object.
order.setCantidad(cant)
orderDao.insert(order)
int lastReg = getLastReg(); // this return the last recort inserted
detail.setOrderId(lastReg)
Hi it depends on the structure of your rows and table.
if you make use of key do this
List<Order> orders = orderDao.queryBuilder()
.orderAsc(Properties.Id)
.limit(1)
.list()
lastOrder = orders[0]

Having trouble creating a Firebase Query requesting only objects with a field of a certain value (in Android)

I have Firebase data that looks like this.
I want to create a Query object in java that retrieves only Invites (those are the objects with randomly generated key's KHcYy...) which have a name = "John Smith".
Here is the code for my current attempt, which is only giving me an empty query:
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.child("brother").equalTo("John Smith");
I tried taking a look through this tutorial on queries, but that only really had info on doing sorty and ranking based queries. I couldn't find any info on this type of task, and the API Reference didn't really make it clear what I'm supposed to be inputting in any of these calls.
Thanks for the help!
Have you tried this...
firebase = new Firebase(ListApplication.FIREBASE_URL);
Firebase objectRef= firebase.child("ExampleParty");
Query q = objectRef.orderByChild("brother").equalTo("John Smith");

How to get the document ID after replicating database

I am currently making a database program using couchbase on android. After I replicating the data, how can I get the document ID so I can list down all the things in database.
You need to make use of Queries!
If you would like to retrieve all the documents (or their IDs), you could use All-documents query.
Query query = database.createAllDocumentsQuery();
QueryEnumerator result = query.run();
for (Iterator<QueryRow> it = result; it.hasNext(); ) {
QueryRow row = it.next();
Log.w("MYAPP", "document ID: %s", row.getDocumentId()); // or QueryRow also has a way to get its source document.
}
You could write your own Views as per your need and later query them for results.
Also look at how to deal with document(s) in a database over here. In fact, I recommend going through entire guide.

Categories

Resources