简体   繁体   中英

Implement SearchView in Action Bar in Android

I have implemented search in action bar, but I am getting 0 results always, even though the text is available. Not sure where I am going wrong?

Here is my piece of code that I have tried:

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

My Search option in Actionbar:

@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 Adaptor:

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

}

You can implements a filter in your custom adapter. Try this: Android Action Bar Search: How to filter a list by the property of a list item?

Hope it helps you!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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