在Android中的Action Bar中实现SearchView

时间:2014-08-20 11:18:22

标签: android searchview android-searchmanager

我已经在操作栏中实现了搜索,但即使文本可用,我总是得到0结果。不知道我哪里出错了?

这是我尝试过的一段代码:

    ArrayList<States> stateList = new ArrayList<States>();

    States _states = new States("AP","Andhra Pradesh",false);
    stateList.add(_states);
    _states = new States("DL","Delhi",true);
    stateList.add(_states);
    _states = new States("GA","Goa",false);
    stateList.add(_states);
    _states = new States("JK","Jammu & Kashmir",true);
    stateList.add(_states);
    _states = new States("KA","Karnataka",true);
    stateList.add(_states);
    _states = new States("KL","Kerala",false);
    stateList.add(_states);
    _states = new States("RJ","Rajasthan",false);
    stateList.add(_states);
    _states = new States("WB","West Bengal",false);
    stateList.add(_states);
    _states = new States("AP","Andhra Pradesh",false);
    stateList.add(_states);
    _states = new States("DL","Delhi",true);
    stateList.add(_states);
    _states = new States("GA","Goa",false);

    //create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(this,R.layout.state_info, stateList);
    listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);
    listView.setTextFilterEnabled(true);

操作栏中的我的搜索选项:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_item_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) 
{
    // this is your adapter that will be filtered
    if (TextUtils.isEmpty(newText))
    {
        listView.clearTextFilter();
    }
    else
    {
        listView.setFilterText(newText.toString());
    }

    return true;
}

MyCustom适配器:

private class MyCustomAdapter extends ArrayAdapter<States>
{

    private ArrayList<States> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId, 

            ArrayList<States> stateList) 
    {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<States>();
        this.stateList.addAll(stateList);
    }

    private class ViewHolder
    {
        TextView code;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null)
        {

            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.state_info, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener( new View.OnClickListener() 
            {
                public void onClick(View v)  
                {
                    CheckBox cb = (CheckBox) v;
                    States _state = (States) cb.getTag();

                    Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), 
                            Toast.LENGTH_LONG).show();

                    _state.setSelected(cb.isChecked());
                }
            });

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        States state = stateList.get(position);

        holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.name.setChecked(state.isSelected());

        holder.name.setTag(state);

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:1)

您可以在自定义适配器中实现过滤器。试试这个:Android Action Bar Search: How to filter a list by the property of a list item?

希望它可以帮到你!!