I want to create a Cursor
, which later I will pass via CursorAdapter
to AutoCompleteTextView
for autocompletion suggestions. The problem is that my SQL statement needs to select 2 different result sets from the same table, just sorted by different criteria. At the same time these result sets must not overlap - otherwise I would have multiple same suggestions. On MySQL, this would be quite easy:
(SELECT field3 FROM table ORDER BY field1 ASC LIMIT 5)
UNION
(SELECT field3 FROM table ORDER BY field2 DESC LIMIT 10)
LIMIT 10
In this case I would have always up to 10 unique values. On SQLite, unfortunately, this is not the case - I cannot have limits on particular selects, just on the whole result set, which makes then UNION not useful for this case at all...
To solve this problem I've thought about making 2 queries separately: iterating through Cursor
from 1st query and putting values into an Array
, then iterating over Cursor
from 2nd query and also putting values into the same array while checking and skipping already existing values in Array
to avoid the duplicates. Then use ArrayAdapter
to pass values to AutoCompleteTextView
.
Theoretically this would work - but what I don't like that everything becomes more complex and much more code to write to solve such a simple task...
Is there maybe a better and easier way to do it?