2012-10-31 3 views
0

저는 John과 같은 이름의 연락처를 나열하기 위해 안드로이드 용 응용 프로그램을 작성했습니다. 하지만 name1 또는 name2과 같은 뷰 연락처가 필요합니다 (예 : Peter OR John).ListView 두 개의 이름으로 필터링하는 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

고맙습니다. 시도해 보겠습니다. –

관련 문제