PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How do you get the members of a contact group?

 November 06, 2022     android, contacts     No comments   

Issue

I have the ID of a contact group, and I'd like to list its members. Here's the code I'm trying:

String[] projection = new String[]{
    ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID 
};
Cursor contacts = getContentResolver().query(
        Data.CONTENT_URI,
        projection,
        ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + gid,
        null,
        null
);
String result = "";
do {
    result += contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)) + " ";
} while (contacts.moveToNext());

But this throws an Exception:

03-24 17:11:33.097: ERROR/AndroidRuntime(10730): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
...
03-24 17:11:33.097: ERROR/AndroidRuntime(10730):     at myapp.MultiSend$1.onItemClick(MultiSend.java:83)

which is the line starting result +=. Can anyone tell me what I'm doing wrong, or suggest a better way to get the same information?


Solution

Try this code snippet

String[] projection = new String[]{
    ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID 
};

Cursor contacts = getContentResolver().query(
        Data.CONTENT_URI,
        projection,
        ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + gid,
        null,
        null
);

startManagingCursor(contacts);

String result = "";

if (contacts.moveToFirst()) {    
      do {    
            try {
                result += contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)) + " ";
            } catch (Exception ex) {
                ex.printStackTrace();
            }
      } while (contacts.moveToNext());
}


Answered By - Muhammad Shahab
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing