首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SearchView安卓

SearchView安卓
EN

Stack Overflow用户
提问于 2015-05-28 20:58:10
回答 1查看 370关注 0票数 0

我是安卓新手,我正在尝试创建一个SearchView。我不明白为什么由于某种原因,负责添加搜索管理器和提供搜索小部件的3行代码会使我的应用程序崩溃。有什么建议吗?

代码语言:javascript
复制
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.option_menu, menu);

    // Add SearchWidget
    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);
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-28 21:30:24

Menu.xml

代码语言:javascript
复制
<!-- Search Widget -->
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.SearchView"
    android:showAsAction="always"
    android:title="Search"/>

activity_search_results.xml

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000" />

</RelativeLayout>

Java类

代码语言:javascript
复制
public class SearchActivity extends Activity {

    private ArrayAdapter<String> myAdapter;
    private ListView listView;
    private String[] dataArray = new String[] { "India", "USA", "UK",
            "Srilanka", "Nepal", "Japan" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        listView = (ListView) findViewById(R.id.listview);
        myAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, dataArray);
        listView.setAdapter(myAdapter);
        listView.setTextFilterEnabled(true);

    }

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

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

        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));
        SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                // this is your adapter that will be filtered
                myAdapter.getFilter().filter(newText);
                System.out.println("on text chnge text: " + newText);
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                // this is your adapter that will be filtered
                myAdapter.getFilter().filter(query);
                System.out.println("on query submit: " + query);
                return true;
            }
        };
        searchView.setOnQueryTextListener(textChangeListener);

        return super.onCreateOptionsMenu(menu);

    }
}

Done

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30507284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档