2016-07-26 178 views
1

你好,我已經在我的應用程序中實現了Autotextview搜索。 我現在面臨的問題是,有時候搜索的字符串之間下面的圖片中進行的:Autotextview搜索功能混淆

enter image description here

就像我寫的「J」在該領域中顯示的值包含‘J’

現在,當我輸入「HDF」 我得到以下結果:

enter image description here

正如你可以在最後的VA看略過來是 「HDF - HDFC BANK LTD」 當我鍵入 'HDF'

現在,當我輸入 「HDFC」

我應該得到 「HDF - HDFC BANK LTD」,因爲它包含了 「HDFC」 但我得到這個結果:

enter image description here

由於有在列表中,即2個值 「HDF - HDFC BANK LTD」 和 「HDFC銀行」。所以「HDFC BANK」顯示正確,但爲什麼我們沒有收到「HDF - HDFC BANK LTD」。爲什麼?

+0

張貼您的xml文件 –

+0

您是在做過濾還是僅向autocompletetextview提供字符串? –

+0

只是應用字符串。 –

回答

0

自定義適配器是這樣的:

這裏List<SearchSuggestion> suggestionList是我的自定義搜索建議類的列表。您可以使用String來進行相應的更改。

public class SearchSuggestionAdapter extends ArrayAdapter<SearchSuggestion> implements Filterable { 

private Context mContext; 
private List<SearchSuggestion> mSuggestionList; 
private List<SearchSuggestion> mFilteredSuggestionList; 

public SearchSuggestionAdapter(Context context, List<SearchSuggestion> suggestionList) { 
    super(context, -1); 
    mContext = context; 
    mSuggestionList = suggestionList; 
    mFilteredSuggestionList = mSuggestionList; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder viewHolder; 
    if (convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.suggestion_item, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.mSuggestionTextView = (TextView) convertView.findViewById(R.id.suggestion_text); 
     viewHolder.mSuggestionImage = (ImageView) convertView.findViewById(R.id.suggestion_image); 
     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    SearchSuggestion searchSuggestion = mFilteredSuggestionList.get(position); 
    viewHolder.mSuggestionTextView.setText(searchSuggestion.getName()); 
    viewHolder.mSuggestionImage.setImageResource(searchSuggestion.isTag() ? R.drawable.suggestion_tag_icon : R.drawable.suggestion_project_icon); 
    return convertView; 
} 

public static class ViewHolder { 
    private ImageView mSuggestionImage; 
    private TextView mSuggestionTextView; 
} 

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

@Override 
public SearchSuggestion getItem(int position) { 
    return mFilteredSuggestionList.get(position); 
} 

@Override 
public Filter getFilter() { 
    return new Filter() { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      final FilterResults results = new FilterResults(); 
      if (constraint != null && constraint.length() > 0) { 
       constraint = constraint.toString().toLowerCase(); 
       List<SearchSuggestion> tempFilteredList = new ArrayList<>(); 
       for (SearchSuggestion suggestion : mSuggestionList) { 
        String name = suggestion.getName(); 
        if (name != null && name.toLowerCase().contains(constraint)) { 
         tempFilteredList.add(suggestion); 
        } 
       } 
       results.values = tempFilteredList; 
       results.count = tempFilteredList.size(); 
      } else { 
       results.values = mSuggestionList; 
       results.count = mSuggestionList.size(); 
      } 
      return results; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      if (results.values != null) { 
       mFilteredSuggestionList = (ArrayList<SearchSuggestion>) results.values; 
       notifyDataSetChanged(); 
      } 
     } 
    }; 
} 
} 
+0

謝謝你的幫助。我會試試這個。但你可以弄清楚這是什麼問題? –

+0

不知道內部發生了什麼,可能是字符串比較問題。自定義適配器像魅力一樣工作。希望這可以幫助。 –