Question

I have some data in a SQLite database and i want to provide basic search capabilities in a listview.

Here is an example of search query I want to be able to solve :

1) Example of sqlite data :

  • ad
  • bc
  • cd
  • d
  • da
  • ec

2) Input & wanted output :

  • a -> [ ad , da ]
  • d -> [ d , da , ad , cd ]
  • c -> [ cd, bc , ec ]

I already thought of a few things but nothing really ok :

  1. Full Text Search : I cannot modify the input db file so I think I cannot use it
  2. one sqlite query (name LIKE ?% OR name LIKE %?%) : not sorted. Perhaps another query I am not aware can be figured out
  3. two differents queries (name LIKE ?% and then name LIKE %?%) : but how to implement it with a content provider ? CursorWrapper ? MergeCursor ? custom Loader ?

I have seen a lot of examples but nothing useful for my custom search.

Thanks for your help.

Edit : Query on d should give [ d , da , ad , cd ].

The custom sort order I want is : LIKE pattern% AND then LIKE %pattern% (for example socks should come before a pair of socks)

Was it helpful?

Solution

Here is some hack for your requirements:

SELECT DISTINCT t.name
FROM (select *, 1 as sec from items WHERE name LIKE 'd%'
union
select *, 2 as sec from items WHERE name LIKE '%d%') as t 
ORDER BY t.sec ASC

OTHER TIPS

The CursorLoader constructor's last parameter is sortOrder - add the name column as your sortOrder to sort the resulting set (note that leaving it blank, as SQLiteDatabase.query may lead to an unsorted order).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top