Hi i have followed official android guideline for enabling search and search suggestions. I works very well but the problem is that it only search from one Database Table.
I have three tables and three Activities with the name “BirthdayWisher_Table”
, “Tasks_Table”
, “Events_Table”
and “BirthdayWisher_Activity”
, “Tasks_Activity”
, “Events_Activity”
respectively.
I want that when the user is in the “BirthdayWisher_Activity” and press the Search menu item my app should search data from the “BirthdayWisher_Table” and when the user is in the “Tasks_Activity” and press the Search menu item my app should search data from the “Tasks_Table”.
But it seems impossible to do so for me.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search"
android:label="@string/app_name"
android:searchSettingsDescription="@string/search_the_entire_app_"
android:searchSuggestAuthority="com.beaconimpex.assistant.app.ContentProvider"
android:searchSuggestIntentAction="android.Intent.action.VIEW"
android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable"
android:searchSuggestPath="BirthdayWishTable"
android:searchSuggestSelection=" suggest_text_1 LIKE ? "
android:searchSuggestThreshold="1" >
</searchable>
<activity
android:name="com.beaconimpex.assistant.SearchAppActivity"
android:label="@string/title_activity_search_app" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable"
android:searchSuggestPath="BirthdayWishTable"
public class AssistantContentProvider extends ContentProvider {
public static final String DATABASE_NAME = "gcuf__dB";
public static final int DATABASE_VERSION = 99;
public static final String AUTHORITY = "com.beaconimpex.assistant.app.ContentProvider";
SQLiteDatabase db;
private DBOpenHelper dbHelper;
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
private static final int SEARCH_SUGGEST = 12345;
static {
sURIMatcher.addURI(AUTHORITY, BirthdayWisher.TABLE_NAME + "/"
+ SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
sURIMatcher.addURI(AUTHORITY, BirthdayWisher.TABLE_NAME + "/"
+ SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Log.i(MyConstants.LOG_TAG, "-----Query method is called with URI: "
+ uri.toString());
switch (sURIMatcher.match(uri)) {
case SEARCH_SUGGEST:
Log.i(MyConstants.LOG_TAG, "URI Search Suggest");
if (selectionArgs != null && selectionArgs.length > 0
&& selectionArgs[0].length() > 0) {
// the entered text can be found in selectionArgs[0]
// return a cursor with appropriate data
return queryCursor(uri, BirthdayWisher.TABLE_NAME,
BirthdayWisher.allColumns, selection,
new String[] { "%" + selectionArgs[0].toLowerCase()
+ "%" }, null);
} else {
// user hasn't entered anything
// thus return a default cursor
Log.i(MyConstants.LOG_TAG,
"User has not entered anything in the searchBox.");
return null;
}
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
But how can i provide custom search suggestions for each Activity by fetching data from related tables using this method?
You simply need to add another searchable file. You can name it whatever you like, so since tasks is missing why not use searchable_tasks.xml
?
Now copy all of the existing searchable.xml
into this file, change the searchSuggestPath
to TasksTable
and do the same for searchSuggestIntentData
.
Of course you have to expand your content provider as well (add those additional possibilities to the URIMatcher and react to the respective URIs in the query method).
you should use the database join for all the table “BirthdayWisher_Table”, “Tasks_Table”, “Events_Table” and “BirthdayWisher_Activity”, “Tasks_Activity”, “Events_Activity” respectively to retrieve all table information. hope this Link could help you to understand what i am talking about. then you can parse all table information in search suggestion
another way to use AutoCompleteTextview
have a look on to this source code
SuggestionAdapter class put the parameter "type" in constructor.
String type;
public SuggestionAdapter(Activity context, String nameFilter,String type) {
super(context, android.R.layout.simple_dropdown_item_1line);
suggestions = new ArrayList<String>();
this.type=type;
}
and then retrieve the information by type on getFilter() method
List<SuggestGetSet> new_suggestions = db.getInformation(type)
then use the AutoCompleteTextView like following
AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete);
acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString(),"Tasks_Table"));
If i am not wrong you can achieve this by:
Pass the activity name with intent to search Activity. Get the called Activity Name from intent. Now you have the called activity name , You can do whatever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With