首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓SearchView Filter ListView

安卓SearchView Filter ListView
EN

Stack Overflow用户
提问于 2012-12-26 17:13:02
回答 4查看 38.7K关注 0票数 20

我已经在我的SherlockAction栏中对我的SearchView实现了搜索过滤器。

当我输入m时,我想在下面只以M开头的列表视图中显示过滤后的结果,依此类推。但现在它显示随机结果。

代码语言:javascript
复制
public boolean onQueryTextChange(String newText) {
    Log.i("Nomad", "onQueryTextChange");

    if (TextUtils.isEmpty(newText)) {
        adapter.getFilter().filter("");
        Log.i("Nomad", "onQueryTextChange Empty String");
        placesListView.clearTextFilter();
    } else {
        Log.i("Nomad", "onQueryTextChange " + newText.toString());
        adapter.getFilter().filter(newText.toString());
    }
    return true;
}

public boolean onQueryTextSubmit(String query) {
    Log.i("Nomad", "onQueryTextSubmit");
    return false;
}

public boolean onClose() {
    Log.i("Nomad", "onClose");
    return false;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-26 17:24:03

将此代码放入适配器中:

代码语言:javascript
复制
@Override
public Filter getFilter(){
   return new Filter(){

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
             constraint = constraint.toString().toLowerCase();
             FilterResults result = new FilterResults();

                if (constraint != null && constraint.toString().length() > 0) {
                  List<String> founded = new ArrayList<String>();
                        for(YourListItemType item: origData){
                            if(item.toString().toLowerCase().contains(constraint)){
                                founded.add(item);
                            }
                    }

                        result.values = founded;
                        result.count = founded.size();
                    }else {
                        result.values = origData;
                        result.count = origData.size();
                    }
            return result;


    }
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
           clear();
           for (String item : (List<String>) results.values) {
                 add(item);
           }
    notifyDataSetChanged();

    }

    }
    }

这个适配器的内部构造函数

代码语言:javascript
复制
public MyAdapter(Context context, int layoutResourceId, String[] places) {
        super(context, layoutResourceId, data);
        this.context = context;

        this.data = Arrays.asList(places);
        this.origData = new ArrayList<String>(this.data);

 }
票数 21
EN

Stack Overflow用户

发布于 2015-03-12 05:41:56

从适配器中获取筛选器,并在查询时更改seachview字段中的字符串。

代码语言:javascript
复制
searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            getListAdapter().getFilter().filter(newText);
            return true;
        }
    });
票数 6
EN

Stack Overflow用户

发布于 2012-12-26 17:40:31

在listview_arr中添加元素,rest代码如下...:-

代码语言:javascript
复制
    listview_arr = new String[listview_array_location.length];
    listview_arr = listview_array;

    setListAdapter(new bsAdapter(this));


    et.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
              // Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s,
                int start, int count, int after)
        {
            // Abstract Method of TextWatcher Interface.
        }
        public void onTextChanged(CharSequence s,int start, int before, int count)
        {
            /*Log.d("count","count==>"+s.length());

                if(((s.length()-temp)%4)==0)
                {
                    Log.d("in if","if in if"+(s.length()-temp));
                    et.setText(et.getText().toString()+" ");
                    int position = et.getText().toString().length();
                    Editable etext = et.getText();
                    Selection.setSelection(etext, position);
                    temp++;
                }*/

            Log.d("count","count==>"+s);
            textlength = et.getText().length();
            array_sort.clear();
            for (int i = 0; i < listview_array_location.length; i++)
            {
                if (textlength <= listview_array_location[i].length())
                {
                    if(et.getText().toString().equalsIgnoreCase((String)listview_array_location[i].subSequence(0,textlength)))
                    {
                        array_sort.add(listview_array[i]);
                    }
                  }
            }
            AppendList(array_sort);
        }
    });
}

public void AppendList(ArrayList<String> str)
{
    listview_arr = new String[str.size()];
    listview_arr = str.toArray(listview_arr);

    setListAdapter(new bsAdapter(this));
}

public class bsAdapter extends BaseAdapter
{
    Activity cntx;
    public bsAdapter(Activity context)
    {
        // TODO Auto-generated constructor stub
        this.cntx=context;

    }

    public int getCount()
    {
        // TODO Auto-generated method stub
        return listview_arr.length;
    }

    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return listview_arr[position];
    }

    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return listview_array.length;
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row=null;

        LayoutInflater inflater=cntx.getLayoutInflater();
        row=inflater.inflate(R.layout.search_list_item, null);

        TextView tv=(TextView)row.findViewById(R.id.title);
        Button Btn01=(Button)row.findViewById(R.id.Btn01);
        Button Btn02=(Button)row.findViewById(R.id.Btn02);

        tv.setText(listview_arr[position]);

        Btn01.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                Toast.makeText(SearchUser.this, "Button 1 "+listview_arr[position], Toast.LENGTH_SHORT).show();
                  int color = PreferenceManager.getDefaultSharedPreferences(
                          SearchUser.this).getInt(COLOR_PREFERENCE_KEY, Color.WHITE);
                  new ColorPickerDialog(SearchUser.this, SearchUser.this, color).show();
            }
         });

        Btn02.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                Toast.makeText(SearchUser.this, "Button 2 "+listview_arr[position], Toast.LENGTH_SHORT).show();
            }
         });

    return row;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14038331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档