自动文字查看功能混淆

时间:2016-07-26 13:03:22

标签: android

您好我已在我的应用程序中实现了自动文本查看。 我面临的问题是,有时搜索是在字符串之间进行的,如下图所示:

enter image description here

就像我写的" J'在该字段中,显示的值包含' J'

现在当我输入" HDF" 我得到以下结果:

enter image description here

正如你在上一次看到的那样,价值即将来临" HDF - HDFC BANK LTD" 当我输入HDF'

现在我输入" HDFC"

我应该得到" HDF - HDFC BANK LTD"因为它包含" HDFC" 但我得到了这个结果:

enter image description here

由于列表中有2个值,即" HDF - HDFC BANK LTD"和" HDFC BANK"。所以" HDFC BANK"显示是正确的,但为什么我们没有得到" HDF - HDFC BANK LTD"。为什么会这样?

1 个答案:

答案 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();
            }
        }
    };
}
}