自动完成与名称和编号为在原生短信应用程序的Andr​​oid [英] AutoComplete with name and number as in native sms app Android

查看:153
本文介绍了自动完成与名称和编号为在原生短信应用程序的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加 AutoCompleteTextView 通过姓名和电话号码我的应用程序和搜索联系人作为在完成与Android原生的短信应用程序。我期待在互联网上,尝试了不少东西,但我想我的应用程序,以完全显示为Android的短信应用程序。这里是code我想只有显示名称的搜索。

I want to add an AutoCompleteTextView in my app and search contacts by name and number as done in the native SMS app with android. I have looked on the internet and tried quite a few things, but I want my application to display it exactly as the android SMS app. Here is the code I am trying that searches only by 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,
        };

}

如何通过电话号码查询呢?然后,显示它是这样的:

How do I search by phone numbers as well? And then display it like this:

推荐答案

我已经做了,在过去类似的东西,这就是我所做的:

I have already done something similar in the past that's what I've done:

注意:录音两种字符后,你会得到自动完成

note: you will get Autocompletion after taping two character

布局文件使用AutoCompleteView,它基本上与你在里面输入时出现的下拉列表中的EditText。在YOUE例如.xml文件看起来是这样的:

The layout file uses an AutoCompleteView, its basically an EditText with a dropdown list that appears as you type in it. The .xml file in youe example looks like this:

<?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使用的自定义视图,你必须声明呼吁custcontview.xml另一个.xml文件,它会是这样的:

To create the custom view used in the AutoCompleteView you have to declare another .xml file called custcontview.xml and it will looks like this:

<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" />

现在在你的活动:

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

不要忘了添加

Don't forget to add

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

在你的清单

这篇关于自动完成与名称和编号为在原生短信应用程序的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆