0

I am new on developing an android app. I am already following the code in this website.I wonder how to connect search on action bar that can search data on sqlite database?

This is my MainActivity.java

    public class MainActivity extends Activity implements SearchView.OnQueryTextListener {
    // More code
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return super.onCreateOptionsMenu(menu);
    }
} 

This is my SearchResultsActivity.java

public class SearchResultsActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handleIntent(getIntent());      
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //super.onNewIntent(intent);
        setIntent(intent);
        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

            ListView listView = (ListView) findViewById(R.id.listView1);
            DatabaseHandler db = new DatabaseHandler(this);
            listView.setAdapter(null);
            db.searchContacts(query);

        }
    }

}
  • It would be helpful if you phrased a question, and limited the post to just the section you are asking about. – Glenn Teitelbaum Nov 21 '13 at 01:31
  • I'm sorry about the post. I just want to know how to connect search on action bar and sqlite database. i am very again about the post – user2967134 Nov 21 '13 at 06:32
  • its okay - you should go back and edit the question (deleting everything that isn't needed) and edit the title as well - maybe - `how do you connect search on action bar and sqlite database?` -- you can always add parts back if people ask for more info on something specific – Glenn Teitelbaum Nov 21 '13 at 06:38
  • I'm already edit the question. Thanks for the tips – user2967134 Nov 21 '13 at 07:55

0 Answers0