2014-03-25 20 views
0

你好,我是android开发新手,我在编码方面苦苦挣扎...我必须获得我的设备的完整联系人列表,以便进行动态增长checkbox可以选择的名称 .... 它必须已被选中,也将动态增长 我已经尝试了很多东西,但没有找到任何答案......我必须从中选择联系人按下按钮如何从设备中获取全部联系方式,以便选择可以选择的chech box名称

public void fetchContacts() { 

     String phoneNumber = null; 

     LayoutInflater layoutInflater = 
        (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       final View addView = layoutInflater.inflate(R.layout.contact, null); 

     Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
     String _ID = ContactsContract.Contacts._ID; 
     String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
     String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

     Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
     String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 

     StringBuffer output = new StringBuffer(); 

     ContentResolver contentResolver = getContentResolver(); 

     Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null); 

     // Loop for every contact in the phone 
     if (cursor.getCount() > 0) { 

      while (cursor.moveToNext()) { 

       String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
       String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 

       int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

       if (hasPhoneNumber > 0) { 

        output.append("\n Name:" + name); 

        // Query and loop for every phone number of the contact 
        Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null); 

        while (phoneCursor.moveToNext()) { 
         phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
        // String nam[]=new String[]{name}; 
        // Toast.makeText(getApplicationContext(), nam[0],Toast.LENGTH_LONG).show(); 
         ch.setText(phoneNumber); 
        // t1.setText(name); 
         ch.setChecked(true); 

        } 

        phoneCursor.close(); 
+0

的内容提供商什么是你这段代码的电流输出? – Aneez

回答

0

我尝试此代码,以获取我的联系

try { 
      String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" 
        + Contacts.HAS_PHONE_NUMBER + "=1) AND (" 
        + Contacts.DISPLAY_NAME + " != ''))"; 

      Cursor c = cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select, 
        null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); 


      for(int i=0;i<c.getCount();i++) 
      { 
//    contactWrap.clear(); 
       try { 
        contactId = 0; 
        String hasPhone = ""; 
        display_name = ""; 
        phoneNumber = ""; 

        c.moveToPosition(i); 

        contactId = c.getLong(0); 
        display_name = c.getString(1); 
        hasPhone = c.getString(7); 

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

        if (Boolean.parseBoolean(hasPhone)) 
        { 
         Cursor phones = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
         while (phones.moveToNext()) 
         { 
          int indexPhoneType = phones.getColumnIndexOrThrow(Phone.TYPE); 
          String phoneType = phones.getString(indexPhoneType); 

          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

          String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 


          contactWrap.add(new ContactsWrapper(contactId, display_name, phoneNumber,lookupKey,false,color_string)); 
         } 
//      map.put(contactId, new ArrayList<ContactsWrapper>(contactWrap)); 
         phones.close(); 
        } 
       } catch (Exception e) { 

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

乐施会美国可以从这个光标采取的ContactID或者你的代码现在这是我的方法,这将给U触点照片只是通过接触ID在它

public InputStream openPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY); 
    Cursor cursor = getContentResolver().query(photoUri, 
      new String[] {Contacts.Photo.PHOTO}, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    try { 
     if (cursor.moveToFirst()) { 
      byte[] data = cursor.getBlob(0); 
      if (data != null) { 
       return new ByteArrayInputStream(data); 
      } 
     } 
    } finally { 
     cursor.close(); 
    } 
    return null; 
} 

希望这将有助于ü最好的运气花花公子

0

你应该使用接触 通过这个example

相关问题