从操作栏执行搜索

时间:2013-05-30 23:28:07

标签: android toast android-search

我已经成功地将我的操作栏显示在android中。我现在正在尝试实施搜索。我试图从小做起,至少看看我是否可以在尝试使用它之前获取搜索词。

我在这里遵循了这个教程:

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

并创建了一个SearchResultsActivity类,看起来像这样来处理搜索。

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

}

我尝试添加一个吐司,看看当我输入文字时,然后点击搜索是否会弹出一个吐司。现在没有吐司出现。

1 个答案:

答案 0 :(得分:15)

我正在使用操作栏中的搜索小部件处理类似的项目。这只与API 11+兼容所以没有姜饼,但它看起来更好。您仍然需要设置searchable.xml并在清单中添加适当的元数据。我不确定你将如何实现它,但下面的代码加载ListView,然后添加字符串。 onCreateOptionsMenu()部分配置搜索小部件并根据插补文本过滤ListView。希望它有所帮助!

    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);

    }

}