2012-10-31 99 views
0

我已經完成了一個應用程序的Android列表與第一個姓名等於例如約翰的聯繫人。但我需要查看與name1name2的聯繫,例如Peter OR JohnListView ArrayAdapter過濾兩個名字

我試過設置過濾器「Peter OR John」,「Peter || John」,但它不起作用。你能幫我嗎?

+2

顯示您的代碼以便能夠爲您提供幫助。 –

回答

2
you should be write your custom adapter and filter. 



    public class MyAdapter<T extends BaseEntity> extends ArrayAdapter<T> implements Filterable{ 

     private MainClass.ObjectFilter filter; 
     private final List<T> objects; 

     public MyAdapter(List<T> objects){ 
      enter code here 
this.objects = objects; 
      getFilter(); 
     } 

     @Override 
     public Filter getFilter() { 
      if (filter == null){ 
       MainClass ofc = new MainClass(objects); 
       filter = ofc.getObjectFilter(); 
      } 
      return filter; 
     } 

     enter code here 
    } 


    and custom filter 


    public class MainClass<T extends BaseEntity> extends BaseEntity { 

     private List<T> allObjectItemsArray; // all contact list 
     private List<T> filteredObjectItemsArray; // filtered contact list 

     public ObjectFilterMainClass(List<T> objects) 
      { 
      this.allObjectItemsArray = new ArrayList<T>(); 
      this.allObjectItemsArray.addAll(objects); 
      this.filteredObjectItemsArray = new ArrayList<T>(); 
      this.filteredObjectItemsArray.addAll(allObjectItemsArray); 
     } 

     public ObjectFilter getObjectFilter(){ 
     return new ObjectFilter(); 
     } 


     public class ObjectFilter extends Filter { 

     @Override 
     public FilterResults performFiltering(CharSequence constraint) { 
       // constraint search text --> Peter OR John 
       constraint = constraint.toString().toLowerCase(); 
      FilterResults result = new FilterResults(); 
      if (constraint != null && constraint.toString().length() > 0) { 
      ArrayList<T> filteredItems = new ArrayList<T>(); 

      a: for (int i = 0; i < allObjectItemsArray.size(); i++) { 
       T m = allObjectItemsArray.get(i); 
         // cast m to your class 
       String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName(); 

       if (fistnameAndLastName.toLowerCase().contains(constraint)) { 
        filteredItems.add(m); 
        continue a; 
       } 

      } 
      result.count = filteredItems.size(); 
      result.values = filteredItems; 
      } else { 
      synchronized (this) { 
       result.values = allObjectItemsArray; 
       result.count = allObjectItemsArray.size(); 
      } 
      } 
      return result; 
     } 

      @Override 
     public void publishResults(CharSequence constraint, FilterResults results) { 

      filteredObjectItemsArray = (ArrayList<T>) results.values; 
      adapter.notifyDataSetChanged(); 
      adapter.clear(); 
      if (filteredObjectItemsArray != null) { 

      for (int i = 0; i < filteredObjectItemsArray.size(); i++) 
       adapter.add(filteredObjectItemsArray.get(i)); 
      } 
      adapter.notifyDataSetInvalidated(); 
     } 

     } 

    } 

    main point is get first and last name and check the constraint text. 
    after that if true add filteredItems list. 

    String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName(); 

    if (fistnameAndLastName.toLowerCase().contains(constraint)) 
    { 
     filteredItems.add(m); 
     continue a; 


    and use : 

    EditText edt = (EditText) this.view.findViewById(R.id.edtTxtName); 

      edt.addTextChangedListener(new TextWatcher() { 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       addFilterToList(); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
      }); 

    public void addFilterToList() { 

     ListView listView = (ListView) this.view.findViewById(R.id.listView); 

     EditText edt = (EditText) this.view.findViewById(R.id.edtTxtName); 

     MyAdapter myA = (MyAdapter) // 
      (listView.getAdapter()); 

     myA.getFilter().filter(edt.getText()); 

     listView.setAdapter(myA); 
     } 
+0

非常感謝,我會嘗試 –