2012-10-22 85 views
0

我無法完成在我的Android應用程序中獲得研究。 我創建了一個editText,並添加了一個TextWatcher。在我的自定義數組適配器中,我重寫了getFilter函數來過濾結果並更新列表視圖。在一個列表視圖中搜索不起作用

我創建了一個EditText和設置就可以了:

et.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      adapter.getFilter().filter(s.toString());    
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      //ContactActivity.this.adapter.getFilter().filter(arg0);     
     } 

和我ContactAdapter是:

public ContactAdapter(Context context, int textViewResourceId, ArrayList<String> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
    } 

    /* 
    * we are overriding the getView method here - this is what defines how each 
    * list item will look. 
    */ 
    public View getView(int position, View convertView, ViewGroup parent){ 

     // assign the view we are converting to a local variable 
     View v = convertView; 

     // first check to see if the view is null. if so, we have to inflate it. 
     // to inflate it basically means to render, or show, the view. 
     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.list_item, null); 
     } 

     /* 
     * Recall that the variable position is sent in as an argument to this method. 
     * The variable simply refers to the position of the current object in the list. (The ArrayAdapter 
     * iterates through the list we sent it) 
     * 
     * Therefore, i refers to the current Item object. 
     */ 
     String i = objects.get(position); 

     TextView tt = (TextView) v.findViewById(R.id.name); 

     if (i != null) { 

      // This is how you obtain a reference to the TextViews. 
      // These TextViews are created in the XML files we defined. 

      if (tt != null){ 

       tt.setText(i); 
      } 

     } 

     // the view must be returned to our activity 
     return v; 

    } 


    @Override 
    public Filter getFilter() { 
     return new Filter() { 
      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       ArrayList<String> list = (ArrayList<String>) results.values; 
       int size = list.size(); 

       //list.clear(); 
       for (int i = 0; i<list.size();i++){ 
        add(list.get(i)); 
       } 
       notifyDataSetChanged(); 
      } 

      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       ArrayList<String> filteredResults = getFilteredResults(constraint); 

       FilterResults results = new FilterResults(); 
       results.values = filteredResults; 

       return results; 
      } 

      private ArrayList<String> getFilteredResults(CharSequence constraint) { 
       ArrayList<String> names = ContactAdapter.this.objects; 
        ArrayList<String> filteredNames = new ArrayList<String>(); 
        for(int i=0;i< names.size();i++){ 
         if(names.get(i).toLowerCase().startsWith(constraint.toString().toLowerCase())){ 
          filteredNames.add(names.get(i)); 
         } 
        } 

        return filteredNames; 
       } 
     }; 
    } 
} 

建議? 感謝

+0

http://stackoverflow.com/questions/12881546/how-to-search-a-data-from-listview-using-textwatcher/12881577#12881577。應該幫助你。 – Raghunandan

+0

http://stackoverflow.com/questions/8678163/list-filter-custom-adapter-dont-give-result/8678198#8678198 –

回答

0

您可以添加此功能,您的自定義適配器:

@覆蓋公共過濾用getFilter(){

 Filter myFilter = new Filter() { 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       // TODO Auto-generated method stub 
       if (!m_orders.isEmpty()){ 
        clear(); 
       } 

       for (int i = 0; i < tempList.size(); i++) { 
        if(tempList.get(i).getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){ 
         add(tempList.get(i)); 
        } 
       } 

       notifyDataSetChanged(); 
      } 

      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       // TODO Auto-generated method stub 
       return null; 
      } 
     }; 

     return myFilter; 
    } 

,您可以此過濾器的搜索到你綁定的EditText:

et_search .addTextChangedListener(new TextWatcher(){

 @Override 
     public void onTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

      m_adapter.getFilter().filter(s, new FilterListener() { 

       @Override 
       public void onFilterComplete(int count) { 
        // TODO Auto-generated method stub 
        m_adapter.notifyDataSetChanged(); 
       } 
      }); 
     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, 
       int arg2, int arg3) { 

     } 

     @Override 
     public void afterTextChanged(Editable str) { 

     } 
    }); 
+0

它崩潰,導致結果(在publishResults())爲空。 – FrankBr

0

源代碼[main.xml中]是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
       android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical"> 
<EditText android:id="@+id/EditText01" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:hint="Search"> 
</EditText> 

<ListView android:id="@+id/ListView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 
</ListView> 
</LinearLayout> 

源代碼[ListViewSearchExample.java]是

package com.ListViewSearchExample; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 

public class ListViewSearchExample extends Activity 
{ 
private ListView lv; 
private EditText et; 
private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", 
"SIX", "SEVEN", "EIGHT", "NINE", "TEN" }; 
private ArrayList<String> array_sort= new ArrayList<String>(); 
int textlength=0; 

public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

lv = (ListView) findViewById(R.id.ListView01); 
et = (EditText) findViewById(R.id.EditText01); 
lv.setAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, listview_array)); 

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) 
{ 
textlength = et.getText().length(); 
array_sort.clear(); 
for (int i = 0; i < listview_array.length; i++) 
{ 
if (textlength <= listview_array[i].length()) 
{ 
if(et.getText().toString().equalsIgnoreCase(
(String) 
listview_array[i].subSequence(0, 
textlength))) 
{ 
                               array_sort.add(listview_array[i]); 
                           } 
                       } 
                   } 
lv.setAdapter(new ArrayAdapter<String> 
(ListViewSearchExample.this, 
android.R.layout.simple_list_item_1, array_sort)); 
} 
}); 
} 
} 

還可以查看這些鏈接...

http://androidsearchfilterlistview.blogspot.in/

http://software-workshop.eu/content/android-development-creating-custom-filter-listview

http://www.mysamplecode.com/2012/07/android-listview-edittext-filter.html

http://androidcocktail.blogspot.in/2012/04/search-custom-listview-in-android.html