2011-04-15 98 views
1

我編寫了以下代碼以獲取聯繫人詳細信息。問題是我收到的電話號碼顯示爲空。你可以幫我嗎?將電話簿添加到應用程序

private void displayRecords() { 
    // An array specifying which columns to return. 

    String columns[] = new String[] { People.NAME, People.NUMBER_KEY}; 
    Uri mContacts = People.CONTENT_URI; 
    Cursor cur = managedQuery(mContacts, columns, // Which columns to return 
      null, // WHERE clause; which rows to return(all rows) 
      null, // WHERE clause selection arguments (none) 
      null // Order-by clause (ascending by name) 

    ); 
    if (cur.moveToFirst()) { 
     String name = null; 
     String phoneNo = null ; 
     do { 
      // Get the field values 
      name = cur.getString(cur.getColumnIndex(People.NAME)); 
      phoneNo =cur.getString(cur.getColumnIndex(People.NUMBER_KEY)); 
      Toast.makeText(this, name + " " + phoneNo, Toast.LENGTH_LONG).show(); 
     } while (cur.moveToNext()); 
    } 
} 
+0

嘗試,而不是NUMBER_KEY People.NUMBER – adithi 2011-04-15 06:35:20

回答

1

試試這個代碼將有助於ü

people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
     int position=0; 
     Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null); 
     people.moveToFirst(); 
      int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);  
      while(!people.isAfterLast()) { 
       Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null); 
       if(!(o.getCount()>0)) 
       { 
       mConname.add(position, people.getString(nameFieldColumnIndex)); 
       try{ 
        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID)); 
        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
        if (hasPhone.equalsIgnoreCase("1")) 
         hasPhone = "true"; 
        else 
         hasPhone = "false" ; 
        if (Boolean.parseBoolean(hasPhone)) 
        { 
         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
         while (phones.moveToNext()) 
         { 
          String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
          mConno.add(position,phoneNumber); 

         } 
         phones.close(); 
        } 
        if(hasPhone=="false") 
        { mConname.remove(position); 
        } 
        else 
         position++; 
       }  
       catch(Exception e) 
       { 

       } 
      } 
       people.moveToNext(); 
      }