0

I want to put search feature to my app via SearchView in ActionBar - I have ListView with some objects. When the search is submitted i want to show the data which is contained in search query to show in another activity in a ListView. To store the data am I using Parse.com framework. I followed this easy tutorial, but I know that it is not made for searching through ListView items, but it searches throuh globa items - downloaded apps etc.

Setting up SearchView in MainActivity.class

@Override
        public boolean onCreateOptionsMenu(Menu menu) {


            getMenuInflater().inflate(R.menu.main, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);
            mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
            setupSearchView(searchItem);
            return true;
        }

     private void setupSearchView(MenuItem searchItem) {
         SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            if (searchManager != null) {
                List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

                SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
                for (SearchableInfo inf : searchables) {
                    if (inf.getSuggestAuthority() != null
                            && inf.getSuggestAuthority().startsWith("applications")) {
                        info = inf;
                    }
                }
                mSearchView.setSearchableInfo(info);
            }

            mSearchView.setOnQueryTextListener(this);
        }
 @Override
    public boolean onQueryTextChange(String arg0) {
        return false;
    }



    @Override
    public boolean onQueryTextSubmit(String arg0) {
        Toast.makeText(getBaseContext(), "submited", Toast.LENGTH_SHORT).show();
        return false;
    }

My Animal.class - getters and setters (check the //note):

@ParseClassName("Animal")
public class Animal extends ParseObject{

    public Animal(){

      }

      public String getAnimal(){    //I want to search by these objects
          return getString("animal");
      }

      public void setAnimal(String animal){
          put("animal", animal);
      }

      public String getArea(){
          return getString("area");
      }

      public void setArea(String area){
          put("area", area);
      }

      public String getImgUrl(){
          return getString("imgUrl");
      }

      public void setImgUrl(String imgUrl){
          put("imgUrl", imgUrl);
      }

      public String getAbout(){
          return getString("about");
      }

      public void setAbout(String about){
          put("about", about);
      }



}

Do you know, if is there some way how to customize it to search through my items in ListView? Or is there completely different but better way, how to do it?

Thanks in advance

marson
  • 913
  • 10
  • 32

1 Answers1

0

In the onQueryTextSubmit(String query) method reset the data in your Adapter filtering by the query. This is best done by restarting your loader if you are using one and passing the query string as an argument in the bundle.

@Override
public boolean onQueryTextSubmit(String query) {
    Bundle args = new Bundle();
    args.putString(ARG_QUERY, query);
    getLoaderManager().restartLoader(LOADER_ID, args, this);
    return true; // Prevent the search view from sending search intent.
}
Ge3ng
  • 1,875
  • 1
  • 24
  • 38