I am not able to get the filter value from the list.
For the wrong search the list shows blank page, when I enter the right value, only the first value from the list is fetched.
After implementing OnQueryTextListener to my class, I inserted the following code:-
Please help. Thanks in Advance
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
lv.clearTextFilter();
}else{
lv.setFilterText(newText.toString());
}
// TODO Auto-generated method stub
return true;
}
Implementing Filterable in Adapter class
public Filter getFilter(){
return null;
}
I Added the above method, now there is NullPointerException generated.
Check out this tutorial it uses Sherlock Action Bar, it shows exactly what you are trying to do. I have used it and works great. http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/
Related
I have been working on an application where I have 6 tabs each of them having a listview in it. I want to filter the listview in each fragment depending upon the text in SearchView which is part of MainActivity. I have a working code for filtering records written in Listview adapter.
The issue is, whenever I want to search something using searchview, the current fragment listview gets filtered but as soon as I move from one fragment to other, the searchview gets reset and the text written in gets clear. That's why I don't get anything in other fragment to search for.
I have following code logic used
In MainActivity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_order, menu);
if(searchView == null)
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return false;
}
In each of the fragment I have :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_items, container, false);
setHasOptionsMenu(true);
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onPrepareOptionsMenu(menu);
android.view.MenuItem item = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(((OrderActivity) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
sv.setOnQueryTextListener(this);
sv.setIconifiedByDefault(false);
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
if (listAdapter != null) {
listAdapter.filter(newText);
}
return true;
}
Please suggest for solution on above issue.
Thanks in advance.
I am trying to implement a searchView in my app that enables the user to search a text across multiple lists. Something like below:
So far I have been able to implement a searchView that searches for text across a single list and it works great, but I can't think of a way to make it work across multiple lists with the section headers showing.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
// some code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
populateAdapter(query);
return true;
}
});
// return true;
return true;
}
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "cityName", "country"});
for (City city: list) {
if (city.getTitle().toLowerCase().startsWith(query.toLowerCase()))
c.addRow(new Object[]{city.getID(), city.getTitle(), city.getNation()});
}
mAdapter.changeCursor(c);
}
Any help would be appreciated, Thanks !!
You can use this library to have different sections in your RecyclerView (movies, concerts, venues). Then you just need to filter the data of the sections in your onQueryTextChange method.
There is a full example here.
I am trying to implement search functionality for my android application. I am not able to get the text entered in the search bar. I have tried all the methods mentioned in SO. Please let me know what mistake am I doing. I am struggling from a long time.
my code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_activity_for_fragments, menu);
SearchManager searchManager = (SearchManager)
getActivity().getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getActivity().getComponentName()));
enteredText = (String) searchView.getQuery();
Log.d("iFocus", "" + enteredText);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("iFocus", ""+searchView.getQuery());
if (0 != searchView.getQuery().length()) {
String spnId = searchView.getQuery().toString();
setSearchResult(spnId);
} else {
setData();
}
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d("iFocus", ""+newText.toString());
adapter.getFilter().filter(newText);
return true;
}
});
}
All suggestions are welcome. Thanks in advance.
Why don't you use in the overridden method onQueryTextSubmit the parameter that you become to get the query like this:
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("iFocus", query);
if (0 != query.length()) {
setSearchResult(query);
} else {
setData();
}
return true;
}
See my example project with ToolBar and search menu item, min API level = 7:
android-toolbar-search
I did a search widget, which works fine. But when I pressed the back button, onCancel and onDismiss are never call.
In the PCRListActivity.java :
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
searchManager.setOnCancelListener(new SearchManager.OnCancelListener() {
#Override
public void onCancel() {
getLoaderManager().restartLoader(LOADER_ID_PRODUCTS, null, PCRListActivity.this);
}
});
searchManager.setOnDismissListener(new SearchManager.OnDismissListener() {
#Override
public void onDismiss() {
getLoaderManager().restartLoader(LOADER_ID_PRODUCTS, null, PCRListActivity.this);
}
});
return true;
}
So, what am I doing wrong ?
I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.
I guess that close and dissmiss methods just work for Progress Dialogs.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//do what you want to when close the sesarchview
//remember to return true;
return true;
}
I want to create a searchview that when the user typing , a table will be created.
From a little search I understood I need to use this method : setOnQueryTextListener.
I get an error in this line :
search.**setOnQueryTextListener**(new OnQueryTextListener() {
with the message:
The method setOnQueryTextListener(SearchView.OnQueryTextListener) in the type SearchView is not applicable for the arguments (new SearchView.OnQueryTextListener(){}).
Thanks !
public boolean onCreateOptionsMenu(Menu menu)
{
final TableLayout table = (TableLayout) findViewById(R.id.restTable);
getMenuInflater().inflate(R.menu.action_data,menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String namelike) {
if(namelike.length()>0)
{
dataHandler.getRestLike(namelike);
createTableLayout(table);
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return true;
}
});
return true;
}