0

我是开发 android 应用程序的新手。我已经在关注这个网站的代码了。我想知道如何连接可以在 sqlite 数据库上搜索数据的操作栏上的搜索?

这是我的 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);
    }
} 

这是我的 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);

        }
    }

}
4

0 回答 0