Reputation:
I am using the officical Android sample code SearchableDictionary, that gives us a search interface, where you can search for a word and while the user types, suggestions are displayed in a dropdown listview.
When a search suggestion is clicked, an Intent is sent to your searchable activity. The Action field of this Intent is described though your searchable XML like this:
<searchable
...
android:searchSuggestIntentAction = "android.intent.action.VIEW">
and then in the searchable activity:
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction()) {
//a suggestion was clicked... do something about it...
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.setData(intent.getData());
startActivity(myIntent);
finish();
}
my problem is that my NEWACTIVITY requires an id to display the relevant data. this id i can get by getting the text of the item that was clicked (like we get by using getItemAtPosition(pos);
) [a string value]. How do i get it?
Upvotes: 1
Views: 1301
Reputation: 2219
I think a security way to get the String query when the user click in a suggestion is by getting the QUERY from the intent android automatically pass when a search is made.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. Handle the intent on activity startup
handleIntent(getIntent());
}
/**
* This method is necessary if you set android:launchMode="singleTop"
* in your activity Manifest. See the explanation below about singleTop.
* @param query : string the user searched for.
*/
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
// ------------------------------------------------------
// ANSWER IS HERE
// ------------------------------------------------------
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// This will get the String from the clicked suggestion
String query = intent.getStringExtra(SearchManager.QUERY);
displaySearchResults(query);
}
}
/**
* 1. Dismiss the KeyBoard and the SearchView (searchMenuItem.collapseActionView();)
* 2. Switch the container to another fragment;
* 3. Send the query to this new fragment;
*/
private void displaySearchResults(String query) {
dismissKeyBoardAndSearchView();
// Open SearchShowFragment and display the results
SearchShowFragment searchShowFragment = new SearchShowFragment();
Bundle bundle = new Bundle();
bundle.putString(SearchShowFragment.QUERY, query);
searchShowFragment.setArguments(bundle);
switchToFragment(searchShowFragment);
}
It is recomended to set singleTop in your activity manifest. Because, if the user make several searches, one followed by the other, it will prevent the system to create the activity several times, one in the top of the other. Imagine the user make 10 searches and then he start to press the back button, it would go back through all the 10 disturbing steps to reach the start.
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
OBSERVATIONS: I am using a single Activity (MainActivity) and two Fragments, the first fragment display all the items and the second fragment display the items by using the query search. The logic to handle the query, the intent, searchview and so on is all made in the MainActivity and the fragments only receives the query and perform the search.
HOW TO HANDLE THE INTENT WHEN THE USER TYPE IT
Note that this will take care only when the user clicks in a suggestions, this will not work when the user type a word and press enter.
For that, use the method below in your onCreateOptionsMenu().
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflates the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.menu_search);
searchView = (SearchView) searchMenuItem.getActionView();
// Assumes the current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryRefinementEnabled(true);
// -----------------------------------------------------------------------
// Typing a Query
// -----------------------------------------------------------------------
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// Save the query to display recent queries
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(MainActivity.this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
// To protect the user's privacy you should always provide a way to clear his search history.
// Put in the menu a way that he can clear the history with the following line below.
/*suggestions.clearHistory();*/
// Display the results
displaySearchResults(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
Happy coding!
This is from the official google documentation: Creating a Search Interface
Upvotes: 0
Reputation:
i did it.
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToFirst();
String y = cursor.getString(1).trim();
final int x = Integer.valueOf(y) - 1;
myIntent.putExtra("id", y);
Upvotes: 2