Search for offers from different tables in different actions

Hi, I have been following the official Android manual to include search suggestions and searches. I work very well, but the problem is that it only searches from one database table.

I have three tables and three actions with the names "BirthdayWisher_Table" , "Tasks_Table" , "Events_Table" and "BirthdayWisher_Activity" , "Tasks_Activity" , "Events_Activity" respectively.

I want that when the user is in "BirthdayWisher_Activity" and clicks the "Search" menu item, my application should look for data from "BirthdayWisher_Table", and when the user is in "Tasks_Activity" and clicks the "Search" menu item, my application should search data from "Tasks_Table".

But for me it is impossible to do.

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> 

This is how I bind my searchAble operation

  <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 works very well for DayWisherTable (because I pointed out)

 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 special offers for each action by extracting data from related tables using this method?

+6
source share
3 answers

You just need to add another searchable file. You can name it whatever you like, so since there are no tasks, do not use searchable_tasks.xml ?

Now copy all existing searchable.xml to this file, change searchSuggestPath to TasksTable and do the same for searchSuggestIntentData .

Of course, you also need to expand the content provider (add these additional features to the URIMatcher and respond to the corresponding URIs in the request method).

+7
source

You must use the database join for all the Birthday, Tasks_Table, Events_Table, and BirthdayWisher_Activity, Tasks_Activity, Events_Activity tables, respectively, to get all the table information. I hope the Link can help you understand what I'm talking about. then you can analyze all the table information in the search clause

another way to use AutoCompleteTextview

check out this source code

The SuggestionAdapter class places the "type" parameter in the 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 get type information using getFilter ()

 List<SuggestGetSet> new_suggestions = db.getInformation(type) 

then use AutoCompleteTextView as shown below.

  AutoCompleteTextView acTextView = (AutoCompleteTextView)findViewById(R.id.autoComplete); acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString(),"Tasks_Table")); 
+2
source

If I'm not mistaken, you can achieve this:

Pass the name of the activity in order to search for Activity. Get the called name of the activity from the intent. Now you have the name of the activity being called, you can do whatever you want.

+2
source

Source: https://habr.com/ru/post/957019/


All Articles