Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search suggestions from different tables in different activities

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.

SearchAble Configuration file

    <?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>

Here is how i am associating my searchAble activity

    <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>

It is working very well for BirthdayWisherTable (because I have specified)

android:searchSuggestIntentData="content://com.beaconimpex.assistant.app.ContentProvider/BirthdayWishTable"
android:searchSuggestPath="BirthdayWishTable"

My Content Provider

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?

like image 734
Shajeel Afzal Avatar asked Oct 30 '13 10:10

Shajeel Afzal


3 Answers

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).

like image 72
Wolfram Rittmeyer Avatar answered Sep 24 '22 10:09

Wolfram Rittmeyer


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"));
like image 31
Munish Kapoor Avatar answered Sep 22 '22 10:09

Munish Kapoor


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.

like image 39
Subramanian Ramsundaram Avatar answered Sep 25 '22 10:09

Subramanian Ramsundaram