Gerardo Mendez Dot Gerardo Mendez Dot - 24 days ago 17
Android Question

Search for parts of a string in a content provider

Hello I'm implementing a searchview and inside onQueryTextChange everytime the user press a letter I want to search that in my DB. The onQueryTextChange is implemented and working I need help with the search, for example:

String selection = AplicationContract.ArtistEntry.COLUMN_NAME + " LIKE ?";
String[] selectionArgs = { "MAX" };

return new CursorLoader(getContext(),
AplicationContract.ArtistEntry.CONTENT_URI,
AplicationContract.ArtistEntry.PROJECTION,
selection,
selectionArgs,
null);


That returns me a cursor if I have a name "MAX" in my db. But if the names in my db are "MAX","MASON","MEGAN","SEAN" so when I hit the letter M I want to search in my content provider for anyname with the letter M, the result would be "MAX","MASON","MEGAN"

Thank you

Answer

When using like statement, you should code like this demo:

select * from tb where c like "%M%"

just learn how to use it on this site: http://www.w3school.com

In this case, you should correct the params:

String[] selectionArgs = { "M"+"%" };
Comments