刷新为AutoCompleteTextView创建的自定义适配器中的数据

时间:2016-04-03 08:41:24

标签: java android arraylist autocompletetextview

我通过扩展BaseAdapter并实现Filterable接口来创建自定义适配器。当用户键入联系人号码或联系人姓名时,此适配器用于向用户建议电话号码。ArrayList<String> data包含具有&#34;人名:数字&#34;模式的数据。这是从手机中提取的。

除了一个问题外,它正在寻找工作。如果我搜索联系人,请以字母&#34; A&#34;它正确显示数据。如果我删除它并键入&#34; B&#34;再次显示两个联系人都以&#34; A&#34;和&#34; B&#34;。我知道在添加新数据之前我必须在某种方法中清除ArrayList<String> matchedResults(检查代码)但是当我这样做时它给了我一个空的ArrayList。在哪种方法我应该这样做或者有不同的解决方案吗?

这里是代码

public class AutoCompleteAdapter extends BaseAdapter implements Filterable {

private Context context;
private ArrayList<String> data;
private ArrayList<String> matchedResults = new ArrayList<String>();

public AutoCompleteAdapter(Context context, ArrayList<String> namesAndNumbers) {
    this.context = context;
    this.data = namesAndNumbers;
}

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

@Override
public String getItem(int position) {
    return matchedResults.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_layout, parent, false);

    TextView nameTextView = (TextView) view.findViewById(R.id.name);
    TextView numberTextView = (TextView) view.findViewById(R.id.number);

    String[] split = matchedResults.get(position).split(":");

    nameTextView.setText(split[0]);
    numberTextView.setText(split[1]);

    return view;
}


@Override
public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            if(constraint != null || constraint.length() != 0) {

                for (String loop:data) {

                    int charSequenceSize = constraint.length();

                    if(onlyText(constraint.toString()) && !matchedResults.contains(loop)){
                        String[] split = loop.split(":");
                        String substring = split[0].substring(0, charSequenceSize);
                        if (substring.equalsIgnoreCase(constraint.toString())){
                            matchedResults.add(loop);
                        }
                    }else if(onlyNumbers(constraint.toString())){
                        String[] split = loop.split(":");
                        String substring = split[1].substring(0, charSequenceSize);
                        if (substring.equals(constraint.toString()) && !matchedResults.contains(loop) ){
                            matchedResults.add(loop);
                        }
                    }
                }
            }

            results.values = matchedResults;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            results.values = matchedResults;
            notifyDataSetChanged();

        }
    };
}

public boolean onlyText(String text) {

    boolean result = false;

    if (Pattern.matches("[a-zA-Z ]+", text) && text.length() >= 1) {

        result = true;
    }
    return result;
}

public boolean onlyNumbers(String text) {

    boolean result = false;

    if (Pattern.matches("[0-9+]+", text) && text.length() >= 1) {

        result = true;
    }
    return result;
}
}

1 个答案:

答案 0 :(得分:0)

将您的过滤器更新为此

  @Override
public Filter getFilter() {
    return nameFilter;
}

Filter nameFilter = new Filter() {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            matchedResults.clear();
            for (String item : data) {
                if (item.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                    matchedResults.add(item);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = matchedResults;
            filterResults.count = matchedResults.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        ArrayList<String> filteredList = (ArrayList<String>) results.values;
        if (results != null && results.count > 0) {
            data.clear();
            for (String c : filteredList) {
                data.add(c);
            }
            notifyDataSetChanged();
        }
    }
};

所以你的整个代码就像这样:

public class AutoCompleteAdapter extends BaseAdapter implements Filterable {

private Context context;
private ArrayList<String> data;
private ArrayList<String> matchedResults = new ArrayList<String>();

public AutoCompleteAdapter(Context context, ArrayList<String> namesAndNumbers) {
    this.context = context;
    this.data = namesAndNumbers;
}

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

@Override
public String getItem(int position) {
    return matchedResults.get(position);
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_layout, parent, false);

    TextView nameTextView = (TextView) view.findViewById(R.id.name);
    TextView numberTextView = (TextView) view.findViewById(R.id.number);

    String[] split = matchedResults.get(position).split(":");

    nameTextView.setText(split[0]);
    numberTextView.setText(split[1]);

    return view;
}


@Override
public Filter getFilter() {
    return nameFilter;
}

Filter nameFilter = new Filter() {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            matchedResults.clear();
            for (String item : data) {
                if (item.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
                    matchedResults.add(item);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = matchedResults;
            filterResults.count = matchedResults.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        ArrayList<String> filteredList = (ArrayList<String>) results.values;
        if (results != null && results.count > 0) {
            data.clear();
            for (String c : filteredList) {
                data.add(c);
            }
            notifyDataSetChanged();
        }
    }
};

public boolean onlyText(String text) {

    boolean result = false;

    if (Pattern.matches("[a-zA-Z ]+", text) && text.length() >= 1) {

        result = true;
    }
    return result;
}

public boolean onlyNumbers(String text) {

    boolean result = false;

    if (Pattern.matches("[0-9+]+", text) && text.length() >= 1) {

        result = true;
    }
    return result;
}

}

我希望对你有所帮助。