Android 从自动完成文本视图中选择联系人

Android 从自动完成文本视图中选择联系人,android,autocomplete,contact,Android,Autocomplete,Contact,我想使用自动完成文本视图选择联系人以发送短信。我几乎实现了我想要的,但正如你在图中所看到的一分钟内的问题。我怎样才能解决这个问题 活动\u与\u auto.xml联系 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:la

我想使用自动完成文本视图选择联系人以发送短信。我几乎实现了我想要的,但正如你在图中所看到的一分钟内的问题。我怎样才能解决这个问题

活动\u与\u auto.xml联系

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<AutoCompleteTextView
    android:id="@+id/mmWhoNo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="To...." >
</AutoCompleteTextView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/ccontName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/ccontName"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/ccontNo"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#A5000000" />
 </LinearLayout>

custcontview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<AutoCompleteTextView
    android:id="@+id/mmWhoNo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="To...." >
</AutoCompleteTextView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/ccontName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontNo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/ccontName"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#A5000000" />

<TextView
    android:id="@+id/ccontType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/ccontNo"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#A5000000" />
 </LinearLayout>

代码

public class ContactWithAuto extends Activity {

private ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_with_auto);
    mPeopleList = new ArrayList<Map<String, String>>();
    PopulatePeopleList();
    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
    mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
            new String[] { "Name", "Phone", "Type" }, new int[] {
                    R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    mTxtPhoneNo.setAdapter(mAdapter);

}

public void PopulatePeopleList() {
    mPeopleList.clear();
    Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (people.moveToNext()) {
        String contactName = people.getString(people
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people
                .getString(people
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if ((Integer.parseInt(hasPhone) > 0)){
            // You know have the number so now query it like this
            Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
            null, null);
            while (phones.moveToNext()){
                //store numbers and display a dialog letting the user select which.
                String phoneNumber = phones.getString(
                phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.NUMBER));
                String numberType = phones.getString(phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.TYPE));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                if(numberType.equals("0"))
                    NamePhoneType.put("Type", "Work");
                    else
                    if(numberType.equals("1"))
                    NamePhoneType.put("Type", "Home");
                    else if(numberType.equals("2"))
                    NamePhoneType.put("Type",  "Mobile");
                    else
                    NamePhoneType.put("Type", "Other");
                    //Then add this map to the list.
                    mPeopleList.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    startManagingCursor(people);
}

public void onItemClick(AdapterView<?> av, View v, int index, long arg){
    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
    Iterator<String> myVeryOwnIterator = map.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        String value=(String)map.get(key);
        mTxtPhoneNo.setText(value);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_contact_with_auto, menu);
    return true;
}
}
public class ContactWithAuto扩展活动{
私人律师事务所;
私人simpledapter mAdapter;
私有自动完成文本视图mTxtPhoneNo;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u contact\u with\u auto);
mpeoplist=newarraylist();
PopulatePeopleList();
mTxtPhoneNo=(AutoCompleteTextView)findviewbyd(R.id.mmWhoNo);
mAdapter=new simpledapter(这个,mpeoplist,R.layout.custcontview,
新字符串[]{“名称”、“电话”、“类型”},新int[]{
R.id.ccontName、R.id.ccontNo、R.id.ccontType});
mTxtPhoneNo.setAdapter(mAdapter);
}
public void PopulatePeopleList(){
mpeoplist.clear();
Cursor people=getContentResolver().query(
Contacts contract.Contacts.CONTENT_URI,null,null,null,null);
while(people.moveToNext()){
String contactName=people.getString(人
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
String contactId=people.getString(人)
.getColumnIndex(Contacts contract.Contacts._ID));
字符串hasPhone=people
.getString(人)
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER));
if((Integer.parseInt(hasPhone)>0)){
//你知道这个数字,所以现在像这样查询它
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT_ID+“=”+contactId,
空,空);
while(phones.moveToNext()){
//存储数字并显示一个对话框,让用户选择哪个。
String phoneNumber=phones.getString(
phones.getColumnIndex(
Contacts contract.CommonDataTypes.Phone.NUMBER));
String numberType=phones.getString(phones.getColumnIndex(
contacts contract.commonDataTypes.Phone.TYPE));
Map NamePhoneType=newhashmap();
NamePhoneType.put(“Name”,contactName);
NamePhoneType.put(“Phone”,phoneNumber);
if(numberType.equals(“0”))
NamePhoneType.put(“Type”、“Work”);
其他的
if(numberType.equals(“1”))
NamePhoneType.put(“Type”、“Home”);
else if(numberType.equals(“2”))
NamePhoneType.put(“Type”、“Mobile”);
其他的
NamePhoneType.put(“Type”、“Other”);
//然后将此地图添加到列表中。
mpeoplist.add(NamePhoneType);
}
电话。关闭();
}
}
人;
开始管理光标(人);
}
公共链接(AdapterView av、视图v、整型索引、长参数){
Map Map=(Map)av.getItemAtPosition(索引);
迭代器myVeryOwnIterator=map.keySet().Iterator();
while(myVeryOwnIterator.hasNext()){
字符串键=(字符串)myVeryOwnIterator.next();
字符串值=(字符串)map.get(键);
mTxtPhoneNo.setText(值);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u contact\u with\u auto,menu);
返回true;
}
}
图像

public class ContactWithAuto extends Activity {

private ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_with_auto);
    mPeopleList = new ArrayList<Map<String, String>>();
    PopulatePeopleList();
    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
    mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview,
            new String[] { "Name", "Phone", "Type" }, new int[] {
                    R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    mTxtPhoneNo.setAdapter(mAdapter);

}

public void PopulatePeopleList() {
    mPeopleList.clear();
    Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (people.moveToNext()) {
        String contactName = people.getString(people
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people
                .getString(people
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if ((Integer.parseInt(hasPhone) > 0)){
            // You know have the number so now query it like this
            Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
            null, null);
            while (phones.moveToNext()){
                //store numbers and display a dialog letting the user select which.
                String phoneNumber = phones.getString(
                phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.NUMBER));
                String numberType = phones.getString(phones.getColumnIndex(
                ContactsContract.CommonDataKinds.Phone.TYPE));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                if(numberType.equals("0"))
                    NamePhoneType.put("Type", "Work");
                    else
                    if(numberType.equals("1"))
                    NamePhoneType.put("Type", "Home");
                    else if(numberType.equals("2"))
                    NamePhoneType.put("Type",  "Mobile");
                    else
                    NamePhoneType.put("Type", "Other");
                    //Then add this map to the list.
                    mPeopleList.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    startManagingCursor(people);
}

public void onItemClick(AdapterView<?> av, View v, int index, long arg){
    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
    Iterator<String> myVeryOwnIterator = map.keySet().iterator();
    while(myVeryOwnIterator.hasNext()) {
        String key=(String)myVeryOwnIterator.next();
        String value=(String)map.get(key);
        mTxtPhoneNo.setText(value);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_contact_with_auto, menu);
    return true;
}
}

您当前的输出似乎是HashMap.toString方法的标准输出。因此,您应该自己实现HashMap并重写toString方法。

为AutoCompleteTextView添加onItemClickListener,而不是将其作为单独的函数

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

        }



    });

对于AutoCompleteTextView,它可以像@user936414所说的那样很有用,但如果你有最大的应用程序,它会带来问题,对于MultiAutoCompleteTextView,更是如此,因此建议通过创建一个“自定义”哈希映射来覆盖toString方法:

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

}

这就是我在这里所做的,看看这个
public-void-onItemClick(AdapterView-av,View-v,int-index,long-arg){Map-Map=(Map)av.getItemAtPosition(index);迭代器myVeryOwnIterator=Map.keySet().Iterator();而(myVeryOwnIterator.hasNext()){字符串键=(String)myVeryOwnIterator.next();字符串值=(String)map.get(key);mTxtPhoneNo.setText(value);}}
Yes,这有助于在下拉列表中显示项目。但当您点击某个条目时,文本编辑中的文本似乎是通过简单地使用item.toString.define您的哈希映射来填充的:
class ContactMap扩展哈希映射{@override public String toString(){return+;}}}
然后在PopulatePeopleList中:
NamePhoneType=new ContactMap()并最终删除你的onItemClick它应该是多余的。嗨,我正在尝试做一些类似于OP的事情,但有一个代码我不确定。在他的R.layout和R.menu中带有auto.xml的活动\u contact\u是否相同?谢谢@Regnarg No这两个文件不能相同。如果您只想测试自动完成文本视图,您可以在CreateOptions菜单方法上发表评论谢谢您的回复!因此,OnCreateOptions菜单与自动完成文本视图无关?您能将其修改为与
MultiAutoCompleteTextView
一起使用吗?顺便说一句,
startManagingCursor()
已被弃用。顺便说一句,
startManagingCursor()
已被弃用。