2012-10-01 205 views
2

這裏是我的過濾器的代碼:過濾列表視圖

private class NameFilter extends Filter { 

    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     // NOTE: this function is *always* called from a background thread, 
     // and 
     // not the UI thread. 
     constraint = constraint.toString().toLowerCase(); 
     FilterResults result = new FilterResults(); 
     if (constraint != null && constraint.toString().length() > 0) { 
      ArrayList<Place> filt = new ArrayList<Place>(); 
      ArrayList<Place> lItems = new ArrayList<Place>(); 
      synchronized (this) { 
       lItems.addAll(objects); 
      } 
      for (int i = 0, l = lItems.size(); i < l; i++) { 
       Place m = lItems.get(i); 
       if (m.getName().toLowerCase().contains(constraint)) 
        filt.add(m); 
      } 
      result.count = filt.size(); 
      result.values = filt; 
     } 
     else { 
      synchronized (this) { 
       result.values = objects; 
       result.count = objects.size(); 
      } 
     } 
     return result; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, 
      FilterResults results) { 
     // NOTE: this function is *always* called from the UI thread. 
     filtered = (ArrayList<Place>) results.values; 
     notifyDataSetChanged(); 
     clear(); 
     for (int i = 0, l = filtered.size(); i < l; i++) 
      add(filtered.get(i)); 
     notifyDataSetInvalidated(); 
    } 

} 

這裏是我的活動代碼:

lvPlace = (ListView) findViewById(R.id.listView1); 

    final ArrayList<Place> searchResults = GetSearchResults(); 

    filterEditText = (EditText) findViewById(R.id.filter_text); 
    filterEditText.addTextChangedListener(filterTextWatcher); 

    adapter = new PlaceAdapter(this, 0, searchResults); 

    lvPlace.setAdapter(adapter); 
    lvPlace.requestFocus(); 
    lvPlace.setTextFilterEnabled(true); 

private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if (adapter != null) { 
      adapter.getFilter().filter(s.toString().toLowerCase()); 
      filterEditText.getText().toString(); 
     } else { 
      Log.d("filter", "no filter availible"); 
     } 
    } 

}; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    filterEditText.removeTextChangedListener(filterTextWatcher); 
} 

我這個掙扎了一段時間,當我在打字的東西becasue搜索框它工作正常,但一旦我從搜索字段中刪除文本,列表不會返回到初始狀態。 請幫我解決這個問題!

回答

0

publishResults方法試試這個:

protected void publishResults(CharSequence constraint,FilterResults results) 
{ 
    filtered.clear(); 
    filtered = (ArrayList<Place>) results.values; 
    if(filtered.size() < 1) 
     filtered = objects; 

    notifyDataSetChanged(); 

    for (int i = 0, l = filtered.size(); i < l; i++) 
     add(filtered.get(i)); 
    notifyDataSetInvalidated(); 
} 

有了這個代碼,就可以取出else子句中的過濾器的方法。

作爲一個額外的,你也可以讓你的for循環更多的高效利用這樣的循環:

for(Place place : filtered) 
    add(place); 
+0

感謝您的幫助,但它不工作:( –

0

當我在搜索框中輸入的東西它工作正常,但 一旦我從搜索字段列表中刪除了文本,並未返回到 的初始狀態。

沒有看到適配器的代碼,這很可能是因爲您沒有保留對適配器使用的初始值的引用。例如,如果您將帶有數據的ArrayList設置到適配器中,並開始在篩選EditText中輸入字符,這將起作用,因爲通過篩選,您僅使用前一組值的一個子集(它始終可用)。當您從EditText中刪除一個字符時,您需要對上一組值進行過濾,但當該組被替換時,值不再存在,並且過濾中斷。

解決方案是使拷貝的初始值始終具有完整的ArrayList值進行過濾。 ArrayList的一個副本將被適配器使用(這將是在publishResults中用publishResults代替的ArrayList),其中一個將用於通過過濾器進行過濾(該ArrayList將保持不變! )在performFiltering方法中。

+0

感謝您的幫助 –

+0

@HùngPhiCao您是否設法解決問題?如果不添加您使用的適配器的代碼。 – Luksprog