2017-08-02 47 views
0

我有一個用於篩選搜索結果的自定義適配器。這適用於查找自動完成搜索標籤。但是,它已被修改爲在用戶第一次觸摸搜索文本視圖時顯示最近搜索的項目。實現Filterable的Android適配器不顯示結果

TextView.OnTouchListener onTouchListener = new View.OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      searchTextView.setText(""); 
      recentSearches = true; 

      return false; 
     } 
    }; 

還有一個Textwatcher:

TextWatcher onSearchSuggestionTextChanged = new TextWatcher() 
    { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) 
     { 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 
      if (s.length() > 0) 
      { 
      recentSearches = false; 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) 
     { 
      if (!recentSearches && s.length() == 0) 
      { 
       cancelSearch(); 
      } 
     } 
    }; 

搜索的TextView如下初始化:過濾處理通過設置文本爲 「」,在搜索的TextView發起

searchTextView.setAdapter(new SearchAdapter(this)); 
searchTextView.addTextChangedListener(onSearchSuggestionTextChanged); 
searchTextView.setOnTouchListener(onTouchListener); 

而搜索適配器是:

public class SearchAdapter extends BaseAdapter implements Filterable 
{ 
    private Context context; 
    private List<FoundItem> resultList = new ArrayList<FoundItem>(); 
    private List<FoundItem> recentSearches = null; 

    public SearchAdapter(Context context) 
    { 
     this.context = context; 
    } 

    @Override 
    public int getCount() 
    { 
     return resultList.size(); 
    } 

    @Override 
    public FoundItem getItem(int index) 
    { 
     return resultList.get(index); 
    } 

    @Override 
    public long getItemId(int position) 
    { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.simple_dropdown_item, parent, false); 
     } 

     FoundItem item = getItem(position); 
     ((TextView) convertView.findViewById(R.id.text)).setText(item.name()); 

     return convertView; 
    } 

    @Override 
    public Filter getFilter() 
    { 
     Filter filter = new Filter() 
     { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) 
      { 
       FilterResults filterResults = new FilterResults(); 
       if (constraint != null) 
       { 
        List<FoundItem> items = findSuggestions(context, constraint.toString()); 

        // Assign the data to the FilterResults 
        filterResults.values = items; 
        filterResults.count = items.size(); 
       } 
       else 
       { 
        filterResults.values = getRecentSearches(); 
        filterResults.count = getRecentSearches().size(); 
       } 
       return filterResults; 
      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) 
      { 
       if (results != null && results.count > 0) 
       { 
        resultList = (List<FoundItem>) results.values; 
        notifyDataSetChanged(); 
       } 
       else 
       { 
        notifyDataSetInvalidated(); 
       } 
      } 
     }; 
     return filter; 
    } 

    private List<FoundItem> findSuggestions(Context context, String query) 
    { 
     ... 
     return foundItems; 
    } 

    private List<FoundItem> getRecentSearches() 
    { 
     ... 
     return recentSearches; 
    } 

適配器確實會得到最近搜索的結果,所以performFilteringpublishResults方法按預期調用,但只調用一次getCount方法,並且不顯示任何結果。相比之下,當輸入幾個字符時,自動完成項目按預期顯示。請注意,最近的搜索不爲空,並且在文本更改前後調用 searchTextView.getAdapter().getCount()時,會返回以前搜索的正確數目。

那爲什麼不顯示結果呢?有任何想法嗎?

感謝

回答

0

手動我們也必須調用

onSearchSuggestionTextChanged.onTextChanged() 

的EditText上的ontouch(ACTION_DOWN)內,如果它的文本長度也爲零!

這將inturn消防過濾器的所有事件

TextView.OnTouchListener onTouchListener = new View.OnTouchListener() 
{ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     searchTextView.setText(""); 
     recentSearches = true; 
     onSearchSuggestionTextChanged.onTextChanged("",0,0,0); 
     return false; 
    } 
};