从地址簿中检索HOME联系号码

时间:2011-08-01 10:43:33

标签: java android

目前我有一个应用程序将检索所有联系人 细节,并将显示所有可用的联系人姓名。但现在我想要检索主页,工作号码。我到处搜索这个,但我不能。怎么能 我实现了这个?请帮帮我。

感谢。

我使用此代码获取Home Numbers。

         Cursor c = getContentResolver().query(Data.CONTENT_URI,
                new String[] {Data._ID, Phone.NUMBER, Data.MIMETYPE},
                 Data.RAW_CONTACT_ID + "=?" + " AND "
                          + Data.MIMETYPE + "='" + Phone.TYPE_HOME + "'",
                 new String[] {String.valueOf(contactId)}, null);

但我需要的是使用单一查询获取家庭,工作,移动(带姓名,电子邮件地址等)号码。

使用此代码总是返回type = 0

Cursor c = getContentResolver().query(Data.CONTENT_URI,
                  null,
                  Data.CONTACT_ID + "=?",
                  new String[] {String.valueOf(contactId)}, null);

           while(c.moveToNext()){

           int type = c.getInt(c.getColumnIndex(Phone.TYPE));
           ..
           }

3 个答案:

答案 0 :(得分:1)

try this:
  int type = mCursor.getInt(mCursor.getColumnIndex(Phone.TYPE));
       the "type" value will be 1,2,3 or 4 where
        TYPE_HOME = 1;
        TYPE_MOBILE = 2;
        TYPE_WORK = 3;
        TYPE_OTHER = 7;

答案 1 :(得分:0)

在查询的where子句中使用Phone.TYPE_HOME ..这将为您提供所需的...

答案 2 :(得分:0)

好的,最后我完成了任务。我的代码

      String mobile = "";
  String home="";
  String work="";
  String fax="";
  String other="";

  String disName="";
  String pName="";
  String fName="";
  String lName="";
  String sName="";
  String mName="";

  String postBox="";
  String streat="";
  String country="";

  String emailAdd="";

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
       while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            disName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));




    ///*   
       Cursor phones = cr.query(Phone.CONTENT_URI, null,
               Phone.CONTACT_ID + " = " + id, null, null);
           while (phones.moveToNext()) {
               String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
               int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
               System.out.println("Numeber= "+number);
               switch (type) {
                   case Phone.TYPE_HOME:
                       home=number;
                       break;
                   case Phone.TYPE_MOBILE:
                       mobile=number;
                       break;
                   case Phone.TYPE_WORK:
                       work=number;
                       break;                       
                   case Phone.TYPE_OTHER:
                       other=number;
                       break;
                   case Phone.TYPE_FAX_HOME:
                       fax=number;
                       break;
                   case Phone.TYPE_FAX_WORK:
                       fax=number;
                       break;
           }              

           }
           phones.close();

           Cursor name = cr.query(Data.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'", null, null);   
           while(name.moveToNext()){
            sName=name.getString(name.getColumnIndex(StructuredName.PREFIX));
            fName=name.getString(name.getColumnIndex(StructuredName.GIVEN_NAME));
            mName=name.getString(name.getColumnIndex(StructuredName.MIDDLE_NAME));
            lName=name.getString(name.getColumnIndex(StructuredName.FAMILY_NAME));
            sName=name.getString(name.getColumnIndex(StructuredName.SUFFIX));
            System.out.println(mName);
           }


           Cursor address = cr.query(Data.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);   
           while(address.moveToNext()){
            postBox=address.getString(address.getColumnIndex(StructuredPostal.POBOX));
            streat=address.getString(address.getColumnIndex(StructuredPostal.STREET));
            mName=address.getString(address.getColumnIndex(StructuredPostal.COUNTRY));

            System.out.println(postBox);
           }

           Cursor email = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id , null, null);   
           while(email.moveToNext()){
            emailAdd=email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
           }