23343

Performing 'like' queries in Android Content Provider

Question:

I'm implementing a Content Provider on Android. I'd like my query to look for more than just exact matches--is there a way to specify a 'like' query in the content provider query method?

Current code. Note that mSearchString is a string the user provides--I'm currently using it to query the UserDictionary, and the only way it returns anything is if mSearchString exactly matches a word in the UserDictionary. I'd like to be able to match partial queries (for instance, if apple, aunt and practical are in the dictionary, I'd like to search for "a" and return them all).

mSelectionClause = UserDictionary.Words.WORD + " = ?"; // Moves the user's input string to the selection arguments. // Remember to add code here to check for invalid or malicious input. mSelectionArgs[0] = mSearchString; // Does a query against the table and returns a Cursor object Cursor mCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause, // Either null, or the word the user entered mSelectionArgs, // Either empty, or the string the user entered mSortOrder);

Answer1:

I am using SQLiteDatabase (not using getContentResolver() and I don't know what it is), but I can search for words like a given pattern (named pattern) using this code, so since the syntax looks the same, I imagine it could work for you:

String whereClause = "word like ?"; String[] wildCardReplacements = {pattern}; cursor = database.query(TABLE_NAME, mColumns, whereClause, wildCardReplacements, null, null, null);

So if pattern contains %a%, I'd get "apple, aunt, and, practical" and lots of others.

In case it's not obvious, in your case, something like mSearchString = "%" + mSearchString + "%" would allow user to merely enter a (for example) and get the desired matches.

P.S. database is defined as an android.database.sqlite.SQLiteDatabase.

Recommend

  • HTML completly deletes and ignores control characters out of a String which i got out of a JSON resp
  • jQuery ajax security
  • why isn't the natural height of a Frame updated immediately?
  • Add spaces between words in spaceless string
  • Azure table query partial partitionkey guid match
  • Using HTML/CSS for UI in XNA?
  • Convert two columns Pandas data frame to dictionary of list with first column as keys
  • TSQL Rolling Average of Time Groupings
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • How do I bind multiple properties in an Android layout element
  • Perspective projection, 4 points
  • Cypher - matching two different possible paths and return both
  • how to populate a SQLite database and use that database in phonegap?
  • MySQL performance when updating row with FK
  • Is there some graphical way to create my own configuration file on SonarLint?
  • UIAlertController button function not working
  • Implementing “partial void” in VB
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • how does System.Web.HttpRequest::PathInfo work?
  • Element.tagName for python not working
  • Spring Cloud Microservice Architecture Confusion
  • gspread or such: help me get cell coordinates (not value)
  • How can I set a binding to a Combox in a UserControl?
  • SplFileObject error failed to open stream: No such file or directory
  • print() is showing quotation marks in results
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Google Custom Search with transparent background
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Insert into database using onclick function
  • What is Eclipse's Declaration View used for?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?