Getting contacts from a specific group in android

I am trying to get contacts from a specific group of known id.i'm in order to get the group ID and the names of the contacts in this group. But I can’t get contact numbers. I tried some solutions from google search, but every time I get the phone number is the same as the group ID, if I request a group for phone numbers and contact names using Phone.number, Phone.DISPLAY_NAME.if, I use the method below . I am getting an error please help me figure out what happened in my code.

Code:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.group_contacts); addGroups = (Button) findViewById(R.id.selectGroup); groupAdapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.select_dialog_singlechoice, GroupCursor(), new String[] { ContactsContract.Groups.TITLE }, new int[] { android.R.id.text1 }); setListAdapter(groupAdapter); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); addGroups.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(myGroups.this, Main.class); Bundle carry = new Bundle(); carry.putStringArrayList("numbers", cNumbers); carry.putStringArrayList("name", cNames); intent.putExtras(carry); setResult(RESULT_OK, intent); finish(); } }); } private Cursor GroupCursor() { String[] projection = { ContactsContract.Groups.TITLE, ContactsContract.Groups._ID }; Cursor gCursor = getContentResolver().query( ContactsContract.Groups.CONTENT_URI, projection, null, null, ContactsContract.Groups.TITLE); // int idcolumn = gCursor.getColumnIndex(ContactsContract.Groups._ID); // String id = gCursor.getString(idcolumn); // Log.d(E, "group id : " + id ); return gCursor; } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Log.d(E, "id : " + id); String groupId = Long.toString(id); String[] cProjection = { Phone.NUMBER, Phone.DISPLAY_NAME ,Contacts.DISPLAY_NAME, Contacts._ID}; Cursor groupCursor = getContentResolver().query(Data.CONTENT_URI, cProjection, CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?", new String[]{groupId}, null); if (groupCursor != null) { groupCursor.moveToFirst(); do { int nameCoumnIndex = groupCursor .getColumnIndex(Phone.DISPLAY_NAME); String name = groupCursor.getString(nameCoumnIndex); String cId = groupCursor.getString(groupCursor.getColumnIndex(Contacts._ID)); Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI, new String[]{Phone.NUMBER}, Phone._ID +"="+cId, null, null); numberCursor.moveToFirst(); int numberColumnIndex = numberCursor .getColumnIndex(Phone.NUMBER); Log.d(E, "numberindex : " + numberColumnIndex); String number = numberCursor.getString(numberColumnIndex); cNumbers.add(number); Log.d(E, "contact " + name + ":" + number); cNames.add(name); } while (groupCursor.moveToNext()); } } } 

Journal:

  09-04 17:59:24.943: D/dalvikvm(18084): GC_EXTERNAL_ALLOC freed 23K, 48% free 2844K/5379K, external 1032K/1039K, paused 24ms 09-04 17:59:26.054: D/(18084): id : 14 09-04 17:59:26.074: D/(18084): numberindex : 0 09-04 17:59:26.074: D/AndroidRuntime(18084): Shutting down VM 09-04 17:59:26.074: W/dalvikvm(18084): threadid=1: thread exiting with uncaught exception (group=0x40015568) 09-04 17:59:26.084: E/AndroidRuntime(18084): FATAL EXCEPTION: main 09-04 17:59:26.084: E/AndroidRuntime(18084): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.database.CursorWrapper.getString(CursorWrapper.java:135) 09-04 17:59:26.084: E/AndroidRuntime(18084): at hm.swarna.groupsms.myGroups.onListItemClick(myGroups.java:117) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.ListView.performItemClick(ListView.java:3535) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1827) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Handler.handleCallback(Handler.java:587) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Handler.dispatchMessage(Handler.java:92) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.os.Looper.loop(Looper.java:130) 09-04 17:59:26.084: E/AndroidRuntime(18084): at android.app.ActivityThread.main(ActivityThread.java:3703) 09-04 17:59:26.084: E/AndroidRuntime(18084): at java.lang.reflect.Method.invokeNative(Native Method) 09-04 17:59:26.084: E/AndroidRuntime(18084): at java.lang.reflect.Method.invoke(Method.java:507) 09-04 17:59:26.084: E/AndroidRuntime(18084): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 09-04 17:59:26.084: E/AndroidRuntime(18084): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 09-04 17:59:26.084: E/AndroidRuntime(18084): at dalvik.system.NativeStart.main(Native Method) 09-04 17:59:27.556: I/Process(18084): Sending signal. PID: 18084 SIG: 9 
+4
source share
3 answers

Try using the following code in the onListItemClick method:

 long groupId = id; String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID }; Cursor groupCursor = getContentResolver().query( Data.CONTENT_URI, cProjection, CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND " + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'", new String[] { String.valueOf(groupId) }, null); if (groupCursor != null && groupCursor.moveToFirst()) { do { int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME); String name = groupCursor.getString(nameCoumnIndex); long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID)); Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null); if (numberCursor.moveToFirst()) { int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER); do { String phoneNumber = numberCursor.getString(numberColumnIndex); Log.d("your tag", "contact " + name + ":" + phoneNumber); } while (numberCursor.moveToNext()); numberCursor.close(); } } while (groupCursor.moveToNext()); groupCursor.close(); } 

You need to specify mimetype when requesting contacts belonging to the specified group. And since one contact can have multiple phone numbers, you may need to request all of them.

EDIT: To get all groups with at least one contact with a phone number, you can use the following query:

 ContentResolver cr = getContentResolver(); Uri groupsUri = ContactsContract.Groups.CONTENT_SUMMARY_URI; String where = "((" + ContactsContract.Groups.GROUP_VISIBLE + " = 1) AND (" + ContactsContract.Groups.SUMMARY_WITH_PHONES + "!= 0))"; Cursor cursor = cr.query(groupsUri, null, where, null, null); 

How to get contacts with a phone number from the specified group - I do not know a simple solution.

+5
source

Here is the answer to what you are asking. Get all phone numbers from a specific group #name. This spaghetti, of course, can be shorter if you use some time on it to optimize it a bit.

 public ArrayList<String> getAllNumbersFromGroupId(String navn) { String selection = ContactsContract.Groups.DELETED + "=? and " + ContactsContract.Groups.GROUP_VISIBLE + "=?"; String[] selectionArgs = { "0", "1" }; Cursor cursor = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, selection, selectionArgs, null); cursor.moveToFirst(); int len = cursor.getCount(); ArrayList<String> numbers = new ArrayList<String>(); for (int i = 0; i < len; i++) { String title = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE)); String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Groups._ID)); if (title.equals(navn)) { String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID }; Cursor groupCursor = context.getContentResolver().query( Data.CONTENT_URI, cProjection, CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND " + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'", new String[] { String.valueOf(id) }, null); if (groupCursor != null && groupCursor.moveToFirst()) { do { int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME); String name = groupCursor.getString(nameCoumnIndex); long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID)); Cursor numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null); if (numberCursor.moveToFirst()) { int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER); do { String phoneNumber = numberCursor.getString(numberColumnIndex); numbers.add(phoneNumber); } while (numberCursor.moveToNext()); numberCursor.close(); } } while (groupCursor.moveToNext()); groupCursor.close(); } break; } cursor.moveToNext(); } cursor.close(); return numbers; } 
0
source

my decision

 public static HashMap<String, String> getContactsForGroup(String groupID, Activity activity){ Cursor dataCursor = activity.getContentResolver().query( ContactsContract.Data.CONTENT_URI, new String[]{ // PROJECTION ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME, // contact name ContactsContract.Data.DATA1 // group }, ContactsContract.Data.MIMETYPE + " = ? " + "AND " + // SELECTION ContactsContract.Data.DATA1 + " = ? ", // set groupID new String[]{ // SELECTION_ARGS ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, groupID }, null); dataCursor.moveToFirst(); HashMap<String, String> map = new HashMap<>(); while (dataCursor.moveToNext()) // { String s0 = dataCursor.getString(0); //contact_id String s1 = dataCursor.getString(1); //contact_name String s2 = dataCursor.getString(2); //group_id Log.d("tag", "contact_id: " + s0 + " contact: " + s1 + " groupID: "+ s2); map.put(s0, s1); } return map; } 
0
source

Source: https://habr.com/ru/post/1432265/


All Articles