I was following these links to get the contacts in my application
How to call Android contacts list?
http://www.higherpass.com/Android/Tutorials/Working-Wit
Simply, have a contact data object, and whenever you select any item in list, set value of that contact into this field.
in
AdapterView.onItemClickListener()
{
public void onItemClick(AdapterView list, View view, int position, long id)
{
Contact contact=listContacts.get(position);
selectedContact=contact;//If Done Button on List
//if activity is to be finished on selection
{
Intent intent=new Intent();
intent.putExtra(KEY_NAME, contact.Name);
....
setResult(RESULT_OK, intent);
finish();
}
}
}
Please try this code
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String name, number = "";
String id;
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
Log.i("name ", name + " ");
Log.i("number ", number + " ");
c.moveToNext();
Try the code below:
ContentResolver contactResolver = getContentResolver();
Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?", new String[]{contactName}, null);
if(cursor.getCount() > 0){
cursor.moveToFirst();
do {
String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
}while (cursor.moveToNext() );
}
By seeing the answers, I think you got the answer how to fetch contacts and now you want to get the selected contacts on your activity.
To fetch contact number specific to the contact name:
ContentResolver contactResolver = getContentResolver();
Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?", new String[]{contactName}, null);
if(cursor.getCount() > 0){
cursor.moveToFirst();
do {
String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
}while (cursor.moveToNext() );
}
Note: Here contactName
is the contact name of which you want to fecth contact numbers.
I am assuming that you have shown the contacts with checkbox in ListView and here is your solution to get the contact list selected by the user to your activity:
1. Start your contact activity with startActivityForResult()
.
2. Initialize ArrayList
variable in contact activity say it contactArrayList
.
3. When user checks the checkbox
, add this contact in your contactArrayList
and keep on adding and when unchecks
then remove the contact from the contactArrayList
.
4. When user presses done then set the result ok with the selected contact list which you have added in contactArrayList
like this:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putStringArrayList("contacts", contactArrayList);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
and finish()
this activity.
5. On your calling activity override:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && data != null ){
Bundle bundle = new Bundle();
bundle = data.getExtras();
ArrayList list = bundle.getStringArrayList("contacts");
}
}
Note: The above code was tested over 2.3.3.
In here you have got the cursor c
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null,null, null);
to get the name
String name = c.getString(c.getColumnIndex(People.NAME));
to get the number
String number = c.getString(c.getColumnIndex(People.NUMBER));
You can iterate through the cursor and save them to a list. Create a checkbox list view and bind with that list.