如何在 android 2.1 中使用联系人姓名填充 AutoCompleteTextView

发布于 2024-09-10 06:39:34 字数 150 浏览 2 评论 0原文

我有一个 AutoCompleteTextView,我希望它在输入时自动完成联系人姓名。问题是 Contacts.People 已被弃用。新文档说使用 ContactsContract 但我找不到任何合适的教程。任何人都可以给我一个示例代码或向我指出适当的教程。

谢谢。

I have an AutoCompleteTextView and i want it to autocomplete contact names on input. The problem is that Contacts.People has been deprecated. The new documentation says to use ContactsContract but i can't find any proper tutorial. Can anyone give me a sample code or point me to an appropriate tutorial.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-09-17 06:39:34

Android Contact API For 2.0

授予访问权限

在应用程序可以查询联系人记录之前,必须通过存储在项目根目录中的 AndroidManifest.xml 文件授予访问权限。在uses-sdk语句下面添加以下uses-permission。

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

查询 Android 联系人数据库

检索联系人详细信息

基本联系人信息存储在联系人表中,详细信息存储在各个表中以进行规范化。在Android 2.0中,查询基本联系人记录的URI存储在ContactsContract.Contacts.CONTENT_URI中。
包higherpass.TestContacts;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class TestContacts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                }
            }
        }
    }
}

该应用程序与任何其他 Android 应用程序一样启动。首先在cr中创建一个ContentResolver实例。然后使用 ContentResolver 实例查询数据库并返回包含联系人列表的 Cursor。该查询是针对 ContactsContract.Contacts.CONTENT_URI 中存储的 URI 执行的。接下来检查光标是否包含记录,如果包含则循环遍历它们。记录 ID 字段存储在 id 变量中。稍后它将用作 where 参数。显示名称字段也存储在字符串名称中。

详细了解使用 Android 联系人

Android Contact API For 2.0

Granting Access

Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement.

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

Querying The Android Contact Database

Retrieving Contact Details

Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 2.0 to query the base contact records the URI to query is stored in ContactsContract.Contacts.CONTENT_URI.
package higherpass.TestContacts;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

public class TestContacts extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                }
            }
        }
    }
}

This application starts off as any other Android application. First create a ContentResolver instance in cr. Then use the ContentResolver instance to query the database and return a Cursor with the contacts list. The query is performed against the URI stored in ContactsContract.Contacts.CONTENT_URI. Next check if the cursor contains records and if so loop through them. The record ID field is stored in the id variable. This will be used as a where parameter later. Also the display name field is stored in the string name.

Read more about Working With Android Contacts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文