2012-11-27 67 views
1

我需要在EditText的幫助下以分開的列表視圖執行搜索功能。我用edittext.addTextChangedListener()方法,我得到了解決方案。但EditText框只接受一個字符,當我試圖輸入第二個字符時它會導致關閉。如何在EditText的幫助下在分開的ListView中搜索?

我用下面的代碼。

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)); 
       } 
      } 
     } 
+0

請發佈logcat消息... – Vinay

回答

0

你可以把下面的控制,

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

我覺得你可以利用過濾器類來實現你想要什麼。只需重寫列表適配器的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類的優點在於,它執行對一個單獨的線程濾波。