【问题标题】:How can i search in separated ListView with the help of EditText?如何在 EditText 的帮助下在分离的 ListView 中搜索?
【发布时间】:2012-11-27 07:54:59
【问题描述】:

我需要借助 EditText 在分离的列表视图中执行搜索功能。我使用了 edittext.addTextChangedListener() 方法,我得到了解决方案。但是 EditText 框只接受一个字符,当我尝试输入第二个字符时,它会导致强制关闭。

我使用了以下代码。

edtSearch.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 cs, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        sAdapter.clear();
        textlength = edtSearch.getText().length();   //getting text from EditText
        Log.e("textlength",""+textlength);
        array_name.clear(); array_friendid.clear(); array_status.clear(); array_image.clear();array_thumb.clear();

        for (int i = 0; i < fullname.length; i++) {
            if (textlength <= fullname[i].length()) {
                if(edtSearch.getText().toString().equalsIgnoreCase((String)fullname[i].subSequence(0,textlength))) {
                    Log.e("arrayname",fullname[i]);

                    array_name.add(fullname[i]);       

                    array_friendid.add(friendid[i]);
                    array_status.add(status[i]);
                    array_image.add(imageurl[i]);
                    array_thumb.add(thumbnailurl[i]);
                    array_header.add(fullname[i].substring(0, 1));
                }
            }
        }

【问题讨论】:

  • 请发布 logcat 消息...

标签: android listview search


【解决方案1】:

你可以在下面放一个控件,

if(textlength > 3) 
for (int i = 0; i < fullname.length; i++)
        {

【讨论】:

    【解决方案2】:

    我认为您可以利用 Filter 类来实现您想要的。只需覆盖列表适配器的 getFilter() 方法。下面是示例代码:

    @Override
        public Filter getFilter() {
    
            if(mFilter == null){
                mFilter = new Filter() {
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {                
                FilterResults filterResults;
    
                //Do your search and initialize filterResults
                  ...
    
                return filterResults;
            }
    
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
    
                if(results != null && results.values != null) {
    
                  notifyDataSetChanged();
              }                    
            }
          };
        }
    
        return mFilter;
    }
    

    Filter 类的优点是它在单独的线程上执行过滤。

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2014-05-20
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多