Problem with search suggestions.

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Problem with search suggestions.

Postby achie1266 » Tue Nov 02, 2010 10:34 pm

I am trying to implement search suggestions in my app in the lines of Searchable Dictionary, except that my data comes from a Sqlite Database.

Everything seems to be working except the suggestions. I created a sample animal database with these values {"Lion", "Tiger", "Elephant", "Deer", "Kangaroo"}

Now when I start typing and click on submit button it works fine.

example typing 't' and hitting search returns the list with Tiger and Elephant. But when showing search suggestions it seems to get the correct number of items being returned as it shows those many rows but it does not populate them. When I click on any of the rows I see that its not passing selectionArgs. It is being null.

What might be causing this issue?

Here is my Database class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.achie.gs1;
  2.  
  3. public class SampleDatabase {
  4.  
  5.  
  6.     private static final String DATABASE_NAME = "zoodatabase.db";
  7.     private static final String ANIMALS_TABLE = "animals";
  8.     private static final int DATABASE_VERSION = 2;
  9.  
  10.     private final ZooOpenHelper mDatabaseOpenHelper;
  11.     private static final HashMap<String, String> mColumnMap = buildColumnMap();
  12.  
  13.     public static final String ANIMAL_NAME = "name";
  14.  
  15.     public SampleDatabase(Context context) {
  16.         mDatabaseOpenHelper = new ZooOpenHelper(context);
  17.     }
  18.  
  19.     private static HashMap<String, String> buildColumnMap() {
  20.         HashMap<String, String> map = new HashMap<String, String>();
  21.         map.put(ANIMAL_NAME, ANIMAL_NAME);
  22.         map.put(BaseColumns._ID, "rowid AS "+BaseColumns._ID);
  23.         map.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS "+SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
  24.         map.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS "+SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
  25.  
  26.         return map;
  27.     }
  28.  
  29.     public Cursor getAnimal(String rowId, String[] columns) {
  30.         String selection = "rowid = ?";
  31.         String[] selectionArgs = new String[] {rowId};
  32.         return query(selection, selectionArgs, columns);
  33.     }
  34.  
  35.     public Cursor getAnimalMatches(String query, String[] columns) {
  36.         String selection = ANIMAL_NAME + " LIKE ?";
  37.         String[] selectionArgs = new String[] {"%"+query+"%"};
  38.         return query(selection, selectionArgs, columns);
  39.     }
  40.  
  41.     private Cursor query(String selection, String[] selectionArgs, String[] columns) {
  42.         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  43.         qb.setTables(ANIMALS_TABLE);
  44.         qb.setProjectionMap(mColumnMap);
  45.  
  46.         Cursor cursor = qb.query(mDatabaseOpenHelper.getReadableDatabase(), columns, selection, selectionArgs, null, null, null);
  47.         if(cursor == null) {
  48.             return null;
  49.         } else if (!cursor.moveToFirst()) {
  50.             cursor.close();
  51.             return null;
  52.         }
  53.         return cursor;
  54.     }
  55.  
  56.     private static class ZooOpenHelper extends SQLiteOpenHelper {
  57.  
  58.         private SQLiteDatabase mDatabase;
  59.         private static final String CREATE_ANIMALS_TABLE = "CREATE TABLE "+ANIMALS_TABLE+
  60.             " (_id integer primary key autoincrement, "+ANIMAL_NAME+" text not null);";
  61.         private static final String[] ANIMALS = new String[] {"Lion", "Tiger", "Elephant", "Deer", "Kangaroo"};
  62.  
  63.         public ZooOpenHelper(Context context) {
  64.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  65.         }
  66.  
  67.         public void onCreate(SQLiteDatabase db) {
  68.             mDatabase = db;
  69.             mDatabase.execSQL(CREATE_ANIMALS_TABLE);
  70.             loadDatabase();
  71.         }
  72.  
  73.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  74.  
  75.         private void loadDatabase() {
  76.             new Thread(new Runnable() {
  77.                 @Override
  78.                 public void run() {
  79.                     loadAnimals();
  80.                 }
  81.             }).start();
  82.         }
  83.  
  84.         private void loadAnimals() {
  85.             for (int i=0; i<ANIMALS.length; i++) {
  86.                 ContentValues initialValues = new ContentValues();
  87.                 initialValues.put("name", ANIMALS[i]);
  88.                 mDatabase.insert(ANIMALS_TABLE, null, initialValues);
  89.             }
  90.         }
  91.     }
  92.  
  93. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


Here is my content provider

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.achie.gs1;
  2.  
  3. public class SampleProvider extends ContentProvider{
  4.  
  5.     public static String AUTHORITY = "com.achie.gs1.SampleProvider";
  6.     public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/animals");
  7.  
  8.     public static final String ANIMALS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE+"/vnd.achie.gs1.animal";
  9.     public static final String ANIMAL_ITEM_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE+"/vnd.achie.gs1.animal";
  10.  
  11.     private SampleDatabase mDatabase;
  12.  
  13.     private static final int GET_ANIMALS = 0;
  14.     private static final int GET_ANIMAL_ITEM = 1;
  15.     private static final int ANIMAL_SUGGEST = 2;
  16.     private static final int SHORTCUT_REFRESH = 3;
  17.  
  18.     private static final UriMatcher sUriMatcher = buildUriMatcher();
  19.  
  20.     private static UriMatcher buildUriMatcher() {
  21.         UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
  22.         // Get animals
  23.         matcher.addURI(AUTHORITY, "animals", GET_ANIMALS);
  24.         matcher.addURI(AUTHORITY, "animals/#", GET_ANIMAL_ITEM);
  25.         // To get suggestions
  26.         matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, ANIMAL_SUGGEST);
  27.         matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", ANIMAL_SUGGEST);
  28.  
  29.         matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT, SHORTCUT_REFRESH);
  30.         matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_SHORTCUT+"/*", SHORTCUT_REFRESH);
  31.  
  32.         return matcher;
  33.     }
  34.  
  35.     @Override
  36.     public boolean onCreate() {
  37.         mDatabase = new SampleDatabase(getContext());
  38.         return true;
  39.     }
  40.  
  41.     @Override
  42.     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  43.         switch(sUriMatcher.match(uri)) {
  44.             case ANIMAL_SUGGEST:
  45.                 if (selectionArgs == null){
  46.                     throw new IllegalArgumentException(" selectionArgs must be provided for the uri for suggestion at a:\n "+uri);
  47.                 }
  48.                 return searchAnimals(selectionArgs[0]);
  49.             case GET_ANIMALS:
  50.                 if (selectionArgs == null){
  51.                     throw new IllegalArgumentException(" selectionArgs must be provided for the uri for searching animals at a:\n "+uri);
  52.                 }
  53.                 return searchAnimals(selectionArgs[0]);
  54.             case GET_ANIMAL_ITEM:
  55.                 return getAnimal(uri);
  56.             case SHORTCUT_REFRESH:
  57.                 return refreshShortcut(uri);
  58.             default:
  59.                 throw new IllegalArgumentException("Unknown Uri at a: "+uri);
  60.         }
  61.     }
  62.  
  63.     @Override
  64.     public String getType(Uri uri) {
  65.         switch (sUriMatcher.match(uri)) {
  66.             case GET_ANIMALS:
  67.                 return ANIMALS_MIME_TYPE;
  68.             case GET_ANIMAL_ITEM:
  69.                 return ANIMAL_ITEM_MIME_TYPE;
  70.             case ANIMAL_SUGGEST:
  71.                 return SearchManager.SUGGEST_MIME_TYPE;
  72.             case SHORTCUT_REFRESH:
  73.                 return SearchManager.SHORTCUT_MIME_TYPE;
  74.             default:
  75.                 throw new IllegalArgumentException("unknown Url "+uri);
  76.         }
  77.     }
  78.  
  79.     private Cursor refreshShortcut(Uri uri) {
  80.         String rowId = uri.getLastPathSegment();
  81.         String[] columns = new String[] {
  82.                 BaseColumns._ID,
  83.                 SampleDatabase.ANIMAL_NAME,
  84.                 SearchManager.SUGGEST_COLUMN_SHORTCUT_ID,
  85.                 SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
  86.         };
  87.         return mDatabase.getAnimal(rowId, columns);
  88.     }
  89.  
  90.     private Cursor searchAnimals(String query) {
  91.         query = query.toLowerCase();
  92.         String[] columns = new String[] {BaseColumns._ID, SampleDatabase.ANIMAL_NAME};
  93.         return mDatabase.getAnimalMatches(query, columns);
  94.     }
  95.  
  96.     private Cursor getAnimal(Uri uri) {
  97.         String rowId = uri.getLastPathSegment();
  98.         String[] columns = new String[] {SampleDatabase.ANIMAL_NAME};
  99.         return mDatabase.getAnimal(rowId, columns);
  100.     }
  101.  
  102.     public int delete(Uri uri, String selection, String[] selectionArgs) {return 0;}
  103.     public Uri insert(Uri uri, ContentValues values) {return null;}
  104.     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0;  }
  105. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


Searchable.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <searchable
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:label="@string/search_label"
  5.    android:hint="@string/search_hint"
  6.    android:searchSettingsDescription="@string/settings_description"
  7.    android:searchSuggestAuthority="com.achie.gs1.SampleProvider"
  8.    android:searchSuggestIntentAction="android.intent.action.VIEW"
  9.    android:searchSuggestIntentData="content://com.achie.gs1.SampleProvider/animals"
  10.    android:searchSuggestSelection=" ?"
  11.    android:searchSuggestThreshold="1"
  12.    android:includeInGlobalSearch="true" >
  13. </searchable>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Here is the stacktrace of the error
W/dalvikvm(14530): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
E/AndroidRuntime(14530): FATAL EXCEPTION: main
E/AndroidRuntime(14530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.achie.gs1/com.achie.gs1.AnimalActivity}: java.lang.IllegalArgumentException: selectionArgs must be provided for the uri for searching animals at a:
E/AndroidRuntime(14530): content://com.achie.gs1.SampleProvider/animals
E/AndroidRuntime(14530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
E/AndroidRuntime(14530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
E/AndroidRuntime(14530): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
E/AndroidRuntime(14530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
E/AndroidRuntime(14530): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(14530): at android.os.Looper.loop(Looper.java:144)
E/AndroidRuntime(14530): at android.app.ActivityThread.main(ActivityThread.java:4937)
E/AndroidRuntime(14530): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(14530): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(14530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(14530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(14530): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(14530): Caused by: java.lang.IllegalArgumentException: selectionArgs must be provided for the uri for searching animals at a:
E/AndroidRuntime(14530): content://com.achie.gs1.SampleProvider/animals
E/AndroidRuntime(14530): at com.achie.gs1.SampleProvider.query(SampleProvider.java:61)
E/AndroidRuntime(14530): at android.content.ContentProvider$Transport.query(ContentProvider.java:169)
E/AndroidRuntime(14530): at android.content.ContentResolver.query(ContentResolver.java:245)
E/AndroidRuntime(14530): at android.app.Activity.managedQuery(Activity.java:1527)
E/AndroidRuntime(14530): at com.achie.gs1.AnimalActivity.onCreate(AnimalActivity.java:16)
E/AndroidRuntime(14530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
E/AndroidRuntime(14530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
E/AndroidRuntime(14530): ... 11 more
W/ActivityManager( 108): Force finishing activity com.achie.gs1/.AnimalActivity
W/ActivityManager( 108): Activity pause timeout for HistoryRecord{461f2ee8 com.achie.gs1/.AnimalActivity}

Thank you
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Re: Problem with search suggestions.

Postby sarah.shalaby » Mon May 20, 2013 12:39 am

Did u manage to get it done?
I am running through the same problem :(
sarah.shalaby
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 20, 2013 12:35 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests