AutoCompleteTextView with email domains android

梦想的初衷 提交于 2019-12-03 10:07:25

问题


So I have an autocompletetextview field in my app which I want the user to enter his email address. Now, to help him type it faster and don't make mistakes, I want to suggest him the most commons email domains servers while typing it.

Im using that control with this array

String[] arraymails ={"@gmail.com","@hotmail.com","@yahoo.com","@outlook.com"};  

and this in the oncreate

mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arraymails);  
mEmailView.setAdapter(adapter);

The idea is that, when the user types the "@" character and then "g" it would suggest @gmail.com.

This works fine if i start typing in the textbox directly "@g.." but if i type anything before, like "john@gm" it won't work.

Is there any kind of wildcard character, like a "*@gmail.com" for doing this? or how should i implement it?

Thanks


回答1:


After several days and looking everywhere for the solution, couldn't find it. I'm posting it because it might help someone.

By trial and error and investigating several sites and guides i could find the solution i was looking for.

This are images of what the solution looks like:

The Textbox, you can type whatever you want, john is an example.

Just typing "@" will give you the list of all possible domains

And you could filter a bit more by starting to type the domain name

The code:

CustomFilterAdapter Class

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

}

On the activity onCreate (you can add here whatever you want)

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); 

And that's all.

Good Luck!




回答2:


i think you must create your own adapter by extending BaseAdapter, and in the getView method you must create your own logic there

Example Link



来源:https://stackoverflow.com/questions/22132909/autocompletetextview-with-email-domains-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!