关闭

AutoCompleteTextView自动补全

39人阅读 评论(0) 收藏 举报
分类:
<AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/main_auto"
        android:completionThreshold="1" //写入一个字符就提示
        android:completionHint="提示"
        />

需要建立适配器才能显示数据

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
    autoText.setAdapter(adapter);

在自定义适配器。
因为ArrayAdapter 实现BaseAdapter和实现Filterable过滤器接口

public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter {

所以自定义适配器需要

public class AutoAdapter extends BaseAdapter implements Filterable{
}

首先写实体类有汉字和拼音两个属性

public class Entry {
    private String text;
    private String PinYin;

    public Entry(String text, String pinYin) {
        this.text = text;
        PinYin = pinYin;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getPinYin() {
        return PinYin;
    }

    public void setPinYin(String pinYin) {
        PinYin = pinYin;
    }
}

实现BaseAdapter里的方法和Filterable接口方法

package com.example.hasee.autotest828;

import android.content.Context;
import android.text.TextUtils;
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 java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by hasee on 2016/8/28.
 */
public class AutoAdapter extends BaseAdapter implements Filterable {

    private Context context;
    private List<Entry> list;
    private List<Entry> backup;
    private Filter filter;

    public AutoAdapter(Context context, List<Entry> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = new TextView(context);
        }
        ((TextView) view).setText(list.get(i).getText());//得到实体类中的text属性
        return view;
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new AutoFilter();
        }
        return filter;
    }

    public class AutoFilter extends Filter {//非静态内部类重写下面两个方法

        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            if (backup == null) {
                backup = new ArrayList<>(list);//把原始数据存放到一个新的集合中进行筛选
            }
            FilterResults results = new FilterResults();
            if (!TextUtils.isEmpty(charSequence)) {
                List<Entry> entries = new ArrayList<>(); //集合存放筛选出的内容
                for (Entry e : backup) {
                    String pinYin = e.getPinYin();
                    if (pinYin.startsWith(charSequence.toString())) {//判断拼音的开头字母跟输入的是否相同
                        entries.add(e);
                    }
                }
                results.count = entries.size();
                results.values = entries;
            } else { //当输入为空 列表为空
                results.count = 0;
                results.values = new ArrayList<>();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            list.clear();
            list.addAll((Collection<? extends Entry>) filterResults.values);
            notifyDataSetChanged();
        }
    }
}

加载适配器

 autoText = (AutoCompleteTextView) findViewById(R.id.main_auto);
        List<Entry> list = new ArrayList<>();
        list.add(new Entry("北京", "beijing"));
        list.add(new Entry("河南", "henan"));
        list.add(new Entry("河北", "hebei"));
        list.add(new Entry("郑州", "zhengzhou"));
        list.add(new Entry("广州", "guangzhou"));
        AutoAdapter adapter = new AutoAdapter(this, list);
        autoText.setAdapter(adapter);

运行结果能根据输入的首字母进行筛选出正确结果

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13544次
    • 积分:733
    • 等级:
    • 排名:千里之外
    • 原创:62篇
    • 转载:6篇
    • 译文:0篇
    • 评论:0条
    文章分类