-1

我正在尝试获取一组的所有详细信息(例如 - 家庭)。

编码:

Cursor c = managedQuery(Data.CONTENT_URI, new String[] {
            Contacts.DISPLAY_NAME, Contacts._ID,GroupMembership.CONTACT_ID },
            GroupMembership.GROUP_ROW_ID + " = ?",
            new String[] { groupID }, Phone.TIMES_CONTACTED + " DESC");

获取姓名和照片:

while (c.moveToNext()&&temp_id<30)
        {           
            String id = c.getString(personIdx);

            //c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.e("myTag", id); 

            // get name
            try{
                personNameText = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            }catch (Exception e) {
                e.printStackTrace();
            }
            // get photo
            try{
                bmp = loadContactPhoto(getContentResolver(),Long.parseLong( id));
                bmp = bmp.createScaledBitmap(bmp, getResources().getDrawable(R.drawable.contact).getIntrinsicWidth(),
                        getResources().getDrawable(R.drawable.contact).getIntrinsicHeight(), false);

            }catch (Exception e) {
                // TODO: handle exception
            }

            // get number and email
            try
            {
                String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if ( hasPhone.equalsIgnoreCase("1"))
                    hasPhone = "true";
                else
                    hasPhone = "false" ;

                if (Boolean.parseBoolean(hasPhone)) 
                {   

                    getNumbers(id);
                }
            }catch (Exception e) {
                e.printStackTrace();
            }

            // get email
            String email = "";
            try{
                Cursor emailCur = getContentResolver().query( 
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                        new String[]{id}, null); 

                while (emailCur.moveToNext()) { 
                    email = emailCur.getString(
                            emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                } 
                emailCur.close();


            }catch (Exception e) {
                e.printStackTrace();
            }

插入数据库

temp_id++;

                insertData(temp_id, bitmaptoByteArray(bmp), phoneNamberText, personNameText,
                        email);

这里提到的所有功能都很好用!问题是我只得到人名而不是其他细节。我认为问题是我弄错了身份证。我将不胜感激!

4

1 回答 1

0

managedQuery() 方法中的第二个参数,即投影,指定要返回的列。

此刻你得到:

new String[] {Contacts.DISPLAY_NAME, Contacts._ID,GroupMembership.CONTACT_ID }

要获取其他信息,您需要将它们包含在该参数中。如果您希望联系人显示姓名、电话号码和电子邮件地址,您的投影将如下所示:

new String[] {Contacts.DISPLAY_NAME, Phone.NUMBER, Email.ADDRESS} 

查看CommonDataKinds,了解在联系人数据表中查找内容时使用的常量的概述。

有关更多信息,请阅读

于 2012-06-17T21:01:29.280 回答