几天来四处寻找解决方案,但却找不到。我发布这篇文章是因为它可能会帮助别人。通过多次尝试和调查多个网站和指南,我找到了我正在寻找的解决方案。以下是解决方案的图片:文本框,您可以键入任何内容,例如John。只需键入“@”即可获取所有可能的域列表,并且您可以通过开始输入域名来进行更多筛选。代码:CustomFilterAdapter类。
public class CustomFilterAdapter extends ArrayAdapter<String> {
private final String MY_DEBUG_TAG = "CustomFilterAdapter";
private ArrayList<String> items;
private ArrayList<String> itemsAll;
private ArrayList<String> suggestions;
private int viewResourceId;
public CustomFilterAdapter(Context context, int viewResourceId, ArrayList<String> items) {
super(context, viewResourceId, items);
this.items = items;
this.itemsAll = (ArrayList<String>) items.clone();
this.suggestions = new ArrayList<String>();
this.viewResourceId = viewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
String customer = items.get(position);
if (customer != null) {
TextView customerNameLabel = (TextView)v;
if (customerNameLabel != null) {
customerNameLabel.setText(customer);
}
}
return v;
}
@Override
public Filter getFilter() {
return nameFilter;
}
Filter nameFilter = new Filter() {
public String convertResultToString(Object resultValue) {
String str = (String)resultValue;
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null){
String palabra = constraint.toString();
if(palabra != null && palabra.indexOf("@") != -1) {
String palabra2 = palabra.substring(palabra.indexOf("@"));
String antesArroba;
try{
antesArroba = palabra.substring(0, palabra.indexOf("@"));
}catch (Exception ex)
{
antesArroba ="";
}
suggestions.clear();
for (String customer : itemsAll) {
if(customer.toLowerCase().startsWith(palabra2.toString().toLowerCase())){
suggestions.add(antesArroba+customer);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new 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) {
clear();
for (String c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};
}
在活动的onCreate方法中(您可以在此处添加任何内容)
arraymails = new ArrayList();
arraymails.add("@gmail.com");
arraymails.add("@hotmail.com");
arraymails.add("@yahoo.com");
arraymails.add("@outlook.com");
arraymails.add("@adinet.com.uy");
mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
mEmailView.setText(mEmail);
CustomFilterAdapter adapter = new CustomFilterAdapter(this,android.R.layout.simple_list_item_1,arraymails);
就是这样了。
祝你好运!