您可以简单地使DB查询这样做.
看看这个方法:
private Boolean createGrp(String GroupName) {
// TODO Auto-generated method stub
String s = "";
for (int i = 0; i < InteractiveArrayAdapter.list.size(); i++) {
if (InteractiveArrayAdapter.list.get(i).isSelected()) {
s = s + i + " ";
}
}
String s1 = null;
s1 = editText.getText().toString();
// Check the edittext is empty or not
if (s1.equals("")) {
Toast.makeText(getActivity(), "Please Enter Any Text", Toast.LENGTH_SHORT).show();
return false;
}
// Check the Group is available or not
Cursor groupCursor = null;
String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE
};
groupCursor = getActivity().managedQuery(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[] {s1}, ContactsContract.Groups.TITLE + " ASC");
Log.d("*** Here Counts: ", "** " + groupCursor.getCount());
if (groupCursor.getCount() > 0) {
Toast.makeText(getActivity(), "Group is already available", Toast.LENGTH_SHORT).show();
return false;
} else {
// Toast.makeText(Create_Group_Main_Page.this, "Not available", Toast.LENGTH_SHORT).show();
// Here we create a new Group
try {
ContentValues groupValues = null;
ContentResolver cr = getActivity().getContentResolver();
groupValues = new ContentValues();
groupValues.put(ContactsContract.Groups.TITLE, s1);
cr.insert(ContactsContract.Groups.CONTENT_URI, groupValues);
Log.d("########### Group Creation Finished :", "###### Success");
} catch (Exception e) {
Log.d("########### Exception :", "" + e.getMessage());
return false;
}
}
groupCursor.close();
groupCursor = null;
Log.d(" **** Contacts add to Groups...", "**** Fine");
String groupID = null;
Cursor getGroupID_Cursor = null;
getGroupID_Cursor = getActivity().managedQuery(ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, ContactsContract.Groups.TITLE + "=?", new String[] {s1}, null);
Log.d("**** Now Empty Cursor size:", "** " + getGroupID_Cursor.getCount());
getGroupID_Cursor.moveToFirst();
groupID = (getGroupID_Cursor.getString(getGroupID_Cursor.getColumnIndex("_id")));
Log.d(" **** Group ID is: ", "** " + groupID);
getGroupID_Cursor.close();
getGroupID_Cursor = null;
for (int i = 0; i < InteractiveArrayAdapter.list.size(); i++) {
if (InteractiveArrayAdapter.list.get(i).isSelected()) {
cursor.moveToPosition(i);
String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
long contact = Long.parseLong(contactID);
long group = Long.parseLong(groupID);
addToGroup(contact, group);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.d(" **** Contact Added: ", "* :" + name);
}
}
return true;
}