I am developing an application in which searchview is in main activity's action bar. And in the main activity there are 3 fragments. There is a listview in each fragment. The problem is that how can I sort fragment's list item when searchview is implemented in activity's action bar.
I am using simple cursor adapter and listview having custom row in which there are textview and imageview.
This is the code of searchview in activity
SearchManager SManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
android.support.v7.widget.SearchView searchViewAction = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
searchViewAction.setIconifiedByDefault(true);
android.support.v7.widget.SearchView.OnQueryTextListener textChangeListener = new android.support.v7.widget.SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchViewAction.setOnQueryTextListener(textChangeListener);
And the code of fragment is
Cursor mCursor = getContacts();
getActivity().startManagingCursor(mCursor);
c_adapter = new SimpleCursorAdapter(getActivity(), R.layout.contact_item, mCursor,
new String[]{ContactsContract.Contacts.DISPLAY_NAME
, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts._ID},
new int[]{R.id.username1, R.id.ivuserpicicon1});
if (c_adapter.getCount() == 0) {
Toast.makeText(getActivity(), "No Items Available", Toast.LENGTH_SHORT).show();
}
listview = (ListView) view.findViewById(R.id.listview);
listview.setAdapter(c_adapter);
listview.setTextFilterEnabled(true);
How can I implement searchview. I need little guidance.
Activity to fragment communication is fairly simple when compared to fragment to activity communication. You can check the following link for both the types of communication
http://developer.android.com/training/basics/fragments/communicating.html#Deliver
In your activity you can get an instance of your fragment containing the ListView and invoke all search and sort operations.
Assuming that you have added
android:actionViewClass="android.widget.SearchView"
for your search menu item in your menu layout file, in the onCreateOptionsMenu of your activity you can add
MenuItem searchItem = menu.findItem(R.id.your_search_menu_item);
SearchView actionSearchView = (SearchView) searchItem.getActionView();
actionSearchView.setIconifiedByDefault(false);
actionSearchView.setOnQueryTextListener(//Set up your OnQueryTextListener here);
In the onQueryTextChange method of the OnQueryTextListener, you can invoke your fragments methods for search and sort.
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