android - 与原生短信应用程序 Android 中一样使用名称和号码自动完成

标签 android android-contacts autocompletetextview

我想在我的应用程序中添加一个 AutoCompleteTextView 并按姓名和号码搜索联系人,就像在带有 android 的 native SMS 应用程序中所做的那样。我已经在互联网上查看并尝试了很多东西,但我希望我的应用程序能够像 android SMS 应用程序一样显示它。这是我尝试仅通过 Display_Name 搜索的代码。

public class MakePayment extends Activity {
    private AutoCompleteTextView mAuto;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.makepayment);

        mAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewTest);
        ContentResolver content = getContentResolver();
        Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                PEOPLE_PROJECTION, null, null, null);

        ContactListAdapter adapter = new ContactListAdapter(this, cursor);
        mAuto.setAdapter(adapter);
    }

     public static class ContactListAdapter extends CursorAdapter implements Filterable {
            public ContactListAdapter(Context context, Cursor c) {
                super(context, c);
                mContent = context.getContentResolver();
            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                 final LayoutInflater inflater = LayoutInflater.from(context);
                    final TextView view = (TextView) inflater.inflate(
                            android.R.layout.simple_expandable_list_item_1, parent, false);
                    view.setText(cursor.getString(2));
                    return view;
            }        

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                ((TextView) view).setText(cursor.getString(2));

            }

            @Override
            public String convertToString(Cursor cursor) {
                return cursor.getString(2);
            }

            @Override
            public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
                if (getFilterQueryProvider() != null) {
                    return getFilterQueryProvider().runQuery(constraint);
                }

                StringBuilder buffer = null;
                String[] args = null;
                if (constraint != null) {
                    buffer = new StringBuilder();
                buffer.append("UPPER(");
                buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
                buffer.append(") GLOB ?");
                    args = new String[] { constraint.toString().toUpperCase() + "*" };
                }

                return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION,
                        buffer == null ? null : buffer.toString(), args,
                        null);
            }



            private ContentResolver mContent;           
        }

     private static final String[] PEOPLE_PROJECTION = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.Contacts.DISPLAY_NAME,
        };

}

我还如何通过电话号码进行搜索?然后像这样显示它: enter image description here

最佳答案

我过去已经做过类似的事情,这就是我所做的:

注意:点击两个字符后会出现自动补全

布局文件使用了一个 AutoCompleteView,它基本上是一个带有下拉列表的 EditText,当您在其中键入内容时会出现一个下拉列表。您示例中的 .xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<AutoCompleteTextView

android:id="@+id/mmWhoNo"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="To...."

>

</AutoCompleteTextView>

</LinearLayout>

要创建在 AutoCompleteView 中使用的自定义 View ,您必须声明另一个名为 custcontview.xml 的 .xml 文件,它看起来像这样:

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

<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:textColor="#A5000000"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<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:textColor="#A5000000"
    android:textAppearance="?android:attr/textAppearanceSmall" />

现在在您的 Activity 中:

public class ContactActivity extends Activity {

    private ArrayList<Map<String, String>> mPeopleList;

    private SimpleAdapter mAdapter;
    private AutoCompleteTextView mTxtPhoneNo;

/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

不要忘记添加

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

在你的 list 中

关于android - 与原生短信应用程序 Android 中一样使用名称和号码自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147311/

相关文章:

android - 直接从联系人选择器 Intent 中选择联系人

android - 暴露的下拉菜单不显示项目

android - 是否可以通过收件箱短信打开我的应用程序?

android - 静态队列与共享首选项

android - 如何在 HTML 的帮助下在 Android 和 iPhone 中启动添加新的联系人屏幕,而无需任何服务器端代码?

cordova - 如何指定 Contact.photos 的 URL?

java - 自动完成 fragment 中的文本

android - 防止 AutocompleteTextView 减少结果

java - Android:ScrollView 禁用所有按钮和导航

android - "Cannot find getter/setter for field"由语言特定字符引起