15

我想使用自动完成文本视图选择联系人以发送短信。我几乎已经实现了我想要的,但是你可以在图像中看到一分钟的问题。我该如何解决?

activity_contact_with_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>

custcontview.xml

<?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;
}
}

图片

截屏

4

3 回答 3

16

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

        }



    });

或为您的活动实施 OnItemClickListener 并设置

mTxtPhoneNo.setOnItemClickListener(this);
于 2012-09-13T06:58:33.983 回答
1

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

于 2012-09-13T06:26:49.130 回答
0

使用 AutoCompeleteTextView 就像@user936414 所说的那样,它可能很有用,但如果你有最大的应用程序,它可能会产生问题,甚至更多的是使用 multiAutoCompeleteTextView 所以建议通过创建一个“自定义”HashMap 来覆盖 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();
}

}

并像这样使用它

                    // Using our custom HashMap
                    ContactMap NamePhoneType = new ContactMap();
于 2014-07-04T08:38:03.527 回答