Back

Need help for Pagination in Java with Appwrite

  • 0
  • Android
  • Databases
Akshaydeep
6 Aug, 2023, 06:33

I am trying to implement pagination in recyclerview but all documents data is loaded. I want to limit this data and load data by "timestamp" and limit to 6. I am working on java not kotlin please always put tutorials for java also.

TL;DR
The user is seeking help with implementing pagination in Java using Appwrite. The thread provides an explanation of pagination and offers the following code as an example: ```java import io.appwrite.Client; import io.appwrite.coroutines.CoroutineCallback; import io.appwrite.services.Databases; Client client = new Client(context) .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint .setProject("5df5acd0d48c2"); // Your project ID Databases databases = new Databases(client); page1 = databases.listDocuments( "[DATABASE_ID]", "[COLLECTION_ID
safwan
6 Aug, 2023, 08:16

https://appwrite.io/docs/client/databases?sdk=android-java#databasesListDocuments Have you looked at this by any chance? There are docs for Java on the website as well

Akshaydeep
6 Aug, 2023, 08:17

i have alrady read this

Akshaydeep
6 Aug, 2023, 08:17

i am asking about pagination

Akshaydeep
6 Aug, 2023, 08:17
Akshaydeep
6 Aug, 2023, 08:17

no reference for java there

Akshaydeep
6 Aug, 2023, 08:17

only kotlin

safwan
6 Aug, 2023, 08:30

Okay I see what you mean, but there isn't much difference between languages. If you look through the available examples of Web, Flutter, Android and Apple, you will see that it's pretty much the same code.

The main thing you need to worry about for pagination are the queries you pass into the databases.listDocuments endpoint. I haven't used Kotlin or Java in a long time so can't be a 100% certain about this, but the changes would be very minimal.

safwan
6 Aug, 2023, 08:31

You just need to follow the endpoint guides for the Java SDK, and you should be good

safwan
6 Aug, 2023, 08:37
TypeScript
import io.appwrite.Client;
import io.appwrite.coroutines.CoroutineCallback;
import io.appwrite.services.Databases;

Client client = new Client(context)
    .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
    .setProject("5df5acd0d48c2"); // Your project ID

Databases databases = new Databases(client);

page1 = databases.listDocuments(
    "[DATABASE_ID]",
    "[COLLECTION_ID]",
    [
      Query.limit(25),
      Query.offset(0)
    ]
);

page2 = databases.listDocuments(
    "[DATABASE_ID]",
    "[COLLECTION_ID]",
    [
      Query.limit(25),
      Query.offset(25)
    ]
);
safwan
6 Aug, 2023, 08:37

This should be the code for Java. Keep in mind I haven't used Java in a really long time so there might be a few mistakes

safwan
6 Aug, 2023, 08:40

Regardless, the idea of pagination is that you set the number of documents you want to get in each page, let's say 25. First you get the first 25 documents which is why in the above code, the Query.limit value is set to 25, and Query.offset is set to 0. The offset query is used to skip a specific number of documents from the start. That's why in the second page, we set the offset to 25 because we already got the first 25 documents, and get the next 25 documents.

safwan
6 Aug, 2023, 08:41

I would suggest you to read up on the different queries that Appwrite has to offer and what each of them do. https://appwrite.io/docs/queries#query-class

joeyouss
8 Aug, 2023, 10:07

Hi @Akshaydeep is this resolved for you? if so, I shall close the post

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more