在哪里为搜索视图添加setOnQueryTextListener以过滤列表?

时间:2015-08-30 15:11:28

标签: java android listview filter searchview

我很困惑在哪里添加搜索视图侦听器,因为我在片段中有一个列表。我已将与list相关的所有变量声明为static。适配器也在主活动中声明为静态变量。但是当我在main活动的onCreateOptionsMenu方法中设置onQueryTextListener时,我在适配器上获得了空指针异常。因此我想到了在片段中添加侦听器,因为适配器是片段。所以我将searchview变量声明为一个静态变量,以便我可以在片段中访问它,但是当我在searchview上获得空指针异常时它失败了。任何人都可以指导我...

主要活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  searchView= (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );

    return true;
}

片段代码

public class ViewStock extends Fragment {
    final MainActivity act = (MainActivity) this.getActivity();
    public RecyclerView mRecyclerView;

    public ViewStock() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootview = inflater.inflate(R.layout.fragment_view_stock, container, false);
        act.array_data = new ArrayList<Item>();

        mRecyclerView = (RecyclerView)rootview.findViewById(R.id.my_recycler_view);
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(llm);

        act.db = new DBHandler(getActivity());
        ArrayList<Item> item_array_from_db = act.db.Get_items();

        for (int i = 0; i < item_array_from_db.size(); i++) {

            int idno = item_array_from_db.get(i).getID();
            String name = item_array_from_db.get(i).getItem_name();
            String desc = item_array_from_db.get(i).getItem_desc();
            String qty = item_array_from_db.get(i).getItem_qty();
            Item cnt = new Item();
            cnt.setID(idno);
            cnt.setItem_name(name);
            cnt.setItem_desc(desc);
            cnt.setItem_qty(qty);

            act.array_data.add(cnt);
        }
        act.db.close();
        act.adapter=new ItemAdapter(getActivity(),act.array_data);
        mRecyclerView.setAdapter(act.adapter);

        return rootview;
    }
}

searchview活动

    public class SearchResultsActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            showResults(query);
        }
    }

    private void showResults(String query) {
        // Query your data set and show results
        // ...
    }
}

这样做我没有得到任何结果。请帮忙..

1 个答案:

答案 0 :(得分:0)

请使用以下代码更新您的代码:

首先尝试这个:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.example, menu);

this.menu = menu;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();

    search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));

    search.setOnQueryTextListener(new OnQueryTextListener() { 

        @Override 
        public boolean onQueryTextChange(String query) {

            loadHistory(query);

            return true; 

        } 

    });

}

其次试试这个:

您的ClassName extends Activity实现了SearchView.OnQueryTextListener {

private SearchView mSearchView;
private TextView mStatusView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

setContentView(R.layout.main);

mStatusView = (TextView) findViewById(R.id.status_text);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.searchview_in_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
setupSearchView(searchItem);

return true;
}

private void setupSearchView(MenuItem searchItem) {

if (isAlwaysExpanded()) {
mSearchView.setIconifiedByDefault(false);
} else {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if (searchManager != null) {
List searchables = searchManager.getSearchablesInGlobalSearch();

SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
for (SearchableInfo inf : searchables) {
if (inf.getSuggestAuthority() != null
&& inf.getSuggestAuthority().startsWith("applications")) {
info = inf;
}
}
mSearchView.setSearchableInfo(info);
}

mSearchView.setOnQueryTextListener(this);
}

public boolean onQueryTextChange(String newText) {
mStatusView.setText("Query = " + newText);
return false;
}

public boolean onQueryTextSubmit(String query) {
mStatusView.setText("Query = " + query + " : submitted");
return false;
}

public boolean onClose() {
mStatusView.setText("Closed!");
return false;
}

protected boolean isAlwaysExpanded() {
return false;
}
}