selecting contact from autocomplete textview

一曲冷凌霜 提交于 2019-11-27 07:08:19

Add a onItemClickListener for the AutoCompleteTextView instead of having it as a seperate function.

 mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> av, View arg1, int index,
                long arg3) {
            Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);

            String name  = map.get("Name");
            String number = map.get("Phone");
            mTxtPhoneNo.setText(""+name+"<"+number+">");

        }



    });

or implement OnItemClickListener for your activity and set

mTxtPhoneNo.setOnItemClickListener(this);

The output you currently have seems to be the standard output of HashMap.toString method. So, you should make your own implementation of HashMap and override toString method.

With an AutoCompeleteTextView its can be useful just to do as what @user936414 said, but it can makes problem if you have biggest app, even more with an multiAutoCompeleteTextView so it s recommended to overide toString methode by creating a "custom" HashMap like that :

public class ContactMap extends HashMap<String, String> {

/*
 * (non-Javadoc)
 * 
 * @see java.util.AbstractMap#toString()
 */
@Override
public String toString() {
    if (isEmpty()) {
        return "{}";
    }

    StringBuilder buffer = new StringBuilder(size() * 28);
    Iterator<Map.Entry<String, String>> it = entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String> entry = it.next();
        Object key = entry.getKey();

        if (key == "Name") {
            Object value = entry.getValue();
            buffer.append(value);
        } else {
            if (key == "Phone")
                buffer.append("<");
            Object value = entry.getValue();
            if (value != this) {
                buffer.append(value);
            } else {
                buffer.append("(this Map)");
            }
            if (key == "Phone")
                buffer.append(">");

        }
    }

    return buffer.toString();
}

}

and use it like this

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