4

I have successfully gotten my action bar to show up right in android. I am now trying to implement the search. I am trying to start small and at least see if I can get the search term before I try and do anything with it.

I followed this tutorial here:

http://developer.android.com/training/search/setup.html

and created a SearchResultsActivity class which looks like this to handle the search.

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //possible more code
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //possible more code
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow

            //toast error test
            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, query, duration);
            toast.show();

        }
    }

    ///more code

}

I tried to add a toast to see if when I entered text then clicked search if a toast would pop up. Right now no toast shows up.

Mike
  • 6,751
  • 23
  • 75
  • 132

1 Answers1

14

I am working on a similar project with a search widget in the action bar. This is only compatible with API 11+ so no gingerbread, but it looks nicer. You still have to set up a searchable.xml and add the appropriate meta-data in the manifest. I'm not sure how you are going to implement this, but the code below loads a ListView, then adds the string. The onCreateOptionsMenu() section configures the search widget and filters the ListView based on the imputed text. Hope it helps!

    public class ListOfMathCourses extends ListActivity  {

    ArrayAdapter<String> adapter;
    ListView list;
    String[] math = new String[] {"Pre-Algebra", "Algebra I", "Algebra II", "Geometry", "Calculus"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.startingpoint);
    list = (ListView) findViewById(android.R.id.list);
    adapter = new ArrayAdapter<String>(this, R.layout.listviewrow, math);
    list.setAdapter(adapter);
    getListView().setTextFilterEnabled(true);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        

        getMenuInflater().inflate(R.menu.menu_search, menu);
        //getMenuInflater().inflate(R.menu.action, menu);   

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);   

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
        {
            @Override
            public boolean onQueryTextChange(String newText) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(newText);
                return true;
            }
            @Override
            public boolean onQueryTextSubmit(String query) 
            {
                // this is your adapter that will be filtered
                adapter.getFilter().filter(query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);

        return super.onCreateOptionsMenu(menu);

    }

}

Code_Insanity
  • 2,648
  • 3
  • 17
  • 21
  • I am going to be searching an online database through an api. So I need to retrieve the term from the search box and send it through attached to a url to access an api which will then return json I need to parse and insert into a list view. I guess where in your code is the actual search string that is entered? – Mike May 31 '13 at 00:19
  • 1
    Im not an expert in any of this, but i think the onQueryTextChange() refreshes the list based on user input. Same thing with the onQueryTextSubmit(). They are both refreshing the list based on what you put in the search widget. The strings are the newText and query. They tell what to filter the list by. So if you sent the query through the url, that might work. Take it with a grain of salt. Hope that helps. – Code_Insanity May 31 '13 at 00:32