Search code examples
androidsearchandroid-sqliteandroid-roomandroid-search

How to get the total query count with a PagedListAdapter in Android Room


This follows a logic from a previous question, quoting the relevant code portion here:

I have the following query implemented and it can return any amount of results between 0 up to the number of entries in the database, depends on the query input text:

@Query("SELECT * FROM Conversation JOIN ConversationFts ON Conversation.id == ConversationFts.id WHERE ConversationFts.title LIKE :text GROUP BY Conversation.id")
public abstract DataSource.Factory<Integer, Conversation> search(String text);

Everything works well except for the total number of results.

Since I have configured the LivePagedListBuilder with setInitialLoadSizeHint(15) every time I receive the result list in the returned observer the list its always 15 in size (or less if the query returns fewer results), for the first time the observer is fired for each unique list (that means whenever a unique query input returns a result list). Once the list is scrolled down past the 15 results it loads more automatically as per the PageListAdapter implementation, but I never get any other count other than 15.

searchRepository(myQuery).observe(this, resultList -> {
    Log.i("RESULT", resultList.size()); // size is always 15 or less
});

So how can I get the total amount of results right from the start? My goal is to display to the user how many results the search returned in the search summary, but as it is the current implementation will never say more than 15 results if there are more than 15 results..


Solution

  • Once again the official documentation fails for yet another basic scenario. The only solution I found was to run two identical queries; one to find the total count of results and the other for the actual list of items to display.