AutoCompleteTextView中的弹出框实现模糊查询

本文介绍了如何在AutoCompleteTextView中实现模糊查询功能。关键在于重写ArrayAdapter,将源码中的startsWith方法替换为indexOf方法,以实现字符串中任意位置包含查询前缀的匹配。
摘要由CSDN通过智能技术生成

AutoCompleteTextView中的弹出框实现模糊查询

只需重写AutoCompleteTextView中的arrayadapter 重写
Arrayadapter 源码中的words[k].startsWith(prefixString) 修改成 words[k].indexOf(prefixString)!= -1

这里直接复制源码修改(这个可能是老版的源码,具体哪个版本的忘了),之后直接使用这个adapter就行了:


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

public class SearchPupItenAdapter<T> extends BaseAdapter implements Filterable {
   
    /**
     * Contains the list of objects that represent the data of this
     * ArrayAdapter. The content of this list is referred to as "the array" in
     * the documentation.
     */
    private List<T> mObjects;

    /**
     * Lock used to modify the content of {
   @link #mObjects}. Any write operation
     * performed on the array should be synchronized on this lock. This lock is
     * also used by the filter (see {
   @link #getFilter()} to make a synchronized
     * copy of the original array of data.
     */
    private final Object mLock = new Object();

    /**
     * The resource indicating what views to inflate to display the content of
     * this array adapter.
     */
    private int mResource;

    /**
     * The resource indicating what views to inflate to display the content of
     * this array adapter in a drop down widget.
     */
    private int mDropDownResource;

    /**
     * If the inflated resource is not a TextView, {
   @link #mFieldId} is used to
     * find a TextView inside the inflated views hierarchy. This field must
     * contain the identifier that matches the one defined in the resource file.
     */
    private int mFieldId = 0;

    /**
     * Indicates whether or not {
   @link #notifyDataSetChanged()} must be called
     * whenever {
   @link #mObjects} is modified.
     */
    private boolean mNotifyOnChange = true;

    private Context mContext;

    // A copy of the original mObjects array, initialized from and then used
    // instead as soon as
    // the mFilter ArrayFilter is used. mObjects will then only contain the
    // filtered values.
    private ArrayList<T> mOriginalValues;
    private ArrayFilter mFilter;

    private LayoutInflater mInflater;

    /**
     * Constructor
     *
     * @param context
     *            The current context.
     * @param resource
     *            The resource ID for a layout file containing a TextView to use
     *            when instantiating views.
     */
    public SearchPupItenAdapter(Context context, int resource) {
        init(context, resource, 0, new ArrayList<T>());
    }

    /**
     * Constructor
     *
     * @param context
     *            The current context.
     * @param resource
     *            The resource ID for a layout file containing a layout to use
     *            when instantiating views.
     * @param textViewResourceId
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值