简体   繁体   English

如何在android中的ActionBar上编写SearchView代码

[英]How to code for SearchView on ActionBar in android

I am not able to get the filter value from the list. 我无法从列表中获取过滤器值。 For the wrong search the list shows blank page, when I enter the right value, only the first value from the list is fetched. 对于错误的搜索,列表显示空白页面,当我输入正确的值时,只提取列表中的第一个值。 After implementing OnQueryTextListener to my class, I inserted the following code:- 在我的类中实现OnQueryTextListener之后,我插入了以下代码: -

Please help. 请帮忙。 Thanks in Advance 提前致谢

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.activity_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.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return super.onCreateOptionsMenu(menu);

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




@Override
public boolean onQueryTextChange(String newText) {
    if(TextUtils.isEmpty(newText)){
        lv.clearTextFilter();
    }else{
        lv.setFilterText(newText.toString());
    }
    // TODO Auto-generated method stub
    return true;
}

Implementing Filterable in Adapter class 在Adapter类中实现Filterable

public Filter getFilter(){
    return null;

}

I Added the above method, now there is NullPointerException generated. 我添加了上面的方法,现在生成了NullPointerException。

Check out this tutorial it uses Sherlock Action Bar, it shows exactly what you are trying to do. 查看本教程它使用Sherlock Action Bar,它会准确显示您要执行的操作。 I have used it and works great. 我用过它并且效果很好。 http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/ http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM