首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android中的联系人

android中的联系人
EN

Stack Overflow用户
提问于 2011-02-04 01:53:52
回答 2查看 713关注 0票数 0

我已经使用了下面的code.but,问题是返回results.is需要更多的时间,有什么解决方案吗?

代码语言:javascript
复制
ContentResolver cr = getContentResolver();
        int index=0;
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if (cur.getCount() > 0) 
        {
            phoneNames=new String[cur.getCount()];
            phoneNumbers=new String[cur.getCount()];
        while (cur.moveToNext())
        {
            String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
             name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));



        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {

             phoneNames[index]=name;
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);


                    while (pCur.moveToNext()) 
                    {
                        phoneIndex++;
                        phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        index++;
                    } 
                    pCur.close();

            }     
        }  
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-04 02:28:21

读完代码后,我假设您想要的是一个带有显示名称和各自电话号码的联系人列表。

如果您专门寻找与电话号码相关的数据,我建议您在android.provider.ContactsContract.PhoneLookup上查询并使用单个游标获取结果。以下是您可能感兴趣的字段: DISPLAY_NAME HAS_PHONE_NUMBER NUMBER类型

e.g

代码语言:javascript
复制
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
 resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...

更多详情请参阅this

如果假设不真实,请发布您的要求。

  • 一些快速检查:

代码语言:javascript
复制
1. Select only the required columns and    not all in the first query.
2. Instead of using Integer.parseInt(cur.getString) use  cur.getInt()
3. Use PhoneLookup whenever dealing with phone numbers ( the    number field    gives the raw phone  number instead of    the value stored    in the database    which can contain

-,),(附加它)

4.避免在游标中使用游标。使用包含已在其中实现的连接的应用程序接口,如RawContactsEntity、PhoneLookup。

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2011-02-04 02:10:02

不要在UI线程上执行复杂的数据库查询。你想对结果做什么?如果要在列表中显示内容,请使用CursorAdapter,以便仅在需要时才提取所需内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4889737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档