在Android操作栏中实现SearchView

在Android操作栏中实现SearchView,android,searchview,android-searchmanager,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","

我已经在操作栏中实现了搜索,但是我总是得到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);
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;
    }

}
私有类MyCustomAdapter扩展了ArrayAdapter
{
私有ArrayList状态列表;
公共MyCustomAdapter(上下文,int textViewResourceId,
ArrayList状态列表)
{
super(上下文、textViewResourceId、状态列表);
this.stateList=新的ArrayList();
this.stateList.addAll(stateList);
}
私有类视窗持有者
{
文本视图代码;
复选框名称;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
ViewHolder=null;
Log.v(“ConvertView”,String.valueOf(position));
if(convertView==null)
{
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
convertView=vi.充气(R.layout.state_info,null);
holder=新的ViewHolder();
holder.code=(TextView)convertView.findViewById(R.id.code);
holder.name=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
holder.name.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
复选框cb=(复选框)v;
States _state=(States)cb.getTag();
Toast.makeText(getApplicationContext(),“单击复选框:”+cb.getText()+“是”+cb.isChecked(),
Toast.LENGTH_LONG).show();
_state.setSelected(cb.isChecked());
}
});
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
States state=stateList.get(位置);
holder.code.setText(“(“+state.getCode()+”);
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
持有者名称设置标签(状态);
返回视图;
}
}

您可以在自定义适配器中实现过滤器。试试这个:


希望它能帮助你

@Alejandra:谢谢你的快速回复。我已经看到了这一点,但无法理解最后几行:for(objectobj:listaTotal)-这里的listaTotal是什么?{if(condition){listResult.add(obj);}}@TheDevMan listaTotal是所有要比较的对象的地方。例如,listaTotal具有所有用户和条件,您可以比较user.getNombre().equals(字符串),如果条件为true,则将用户添加到筛选器列表中。我不知道我是否解释过。为我可怜的孩子道歉English@Alejandra:对不起,我的总排名中还有一个NPE?这是我的代码行:我是否在代码中遗漏了什么?listaTotal是什么?Devman listaTotal的一个对象数组将是stateList。你想要状态列表的过滤元素吗?非常感谢我的工作。我不得不改变一些东西来满足我的需要。
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;
    }

}