2011-06-15 16 views
9

すべての連絡先をAndroidの連絡先のお気に入りリストに登録しようとしています。現在、お気に入りのグループIDを含むすべてのグループIDを取得できます。しかし、お気に入りのグループIDとしてグループIDを持つ連絡先はないようです。Androidでお気に入りの連絡先を取得する

私はすべてのグループIDと各グループの連絡先を取得しようとしています。 2つのリストを印刷した後、私は好きなのグループIDは、コンタクトリスト

ArrayList<String> favGroupId=new ArrayList<String>(); 
     final String[] GROUP_PROJECTION = new String[] { 
       ContactsContract.Groups._ID, ContactsContract.Groups.TITLE }; 
     Cursor cursor = getContentResolver().query(
     ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null, 
       null, ContactsContract.Groups.TITLE); 

     while (cursor.moveToNext()) { 
      String id = cursor.getString(cursor 
        .getColumnIndex(ContactsContract.Groups._ID)); 
      Log.v("Test",id); 

      String gTitle = (cursor.getString(cursor 
        .getColumnIndex(ContactsContract.Groups.TITLE))); 

      Log.v("Test",gTitle); 
      if (gTitle.contains("Favorite_")) { 
       gTitle = "Favorites"; 
       favGroupId.add(id); 
      } 
     } 
     cursor.close(); 

答えて

22

あなたはContactsContract.ContactクラスでSTARREDフィールドを使用することができていないことがわかりました。あなたがあなたのクエリを変更する場合:

Cursor cursor = this.managedQuery(
    ContactsContract.Contacts.CONTENT_URI, projection, "starred=?", 
    new String[] {"1"}, null); 

これは、Androidのデフォルトの連絡先アプリに[お気に入り]タブに表示されるすべての連絡先のリストを返す必要があります。

4

テントとの接触を開くためintentUriStringを含む完全な答えは、:

Map getFavoriteContacts(){ 

    Map contactMap = new HashMap(); 

    Uri queryUri = ContactsContract.Contacts.CONTENT_URI; 

    String[] projection = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.STARRED}; 

    String selection =ContactsContract.Contacts.STARRED + "='1'"; 

    Cursor cursor = managedQuery(queryUri, projection, selection, null, null); 

    while (cursor.moveToNext()) { 
     String contactID = cursor.getString(cursor 
       .getColumnIndex(ContactsContract.Contacts._ID)); 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     Uri uri = Uri.withAppendedPath(
      ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID)); 
     intent.setData(uri); 
     String intentUriString = intent.toUri(0); 

     String title = (cursor.getString(
      cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 

     contactMap.put(title,intentUriString); 
    } 

    cursor.close(); 
    return contactMap; 
} 
関連する問題