I have trying to solve this problem for days now and tomorrow I have to hand in this task, but right now I'm a little bit worried that I can't! My problem is that I can't get the email address from a selected contact. I can get the number and the name, but not the email address! I have tested all kinds of alternatives that I have found on different webpages.
In my application I have a listview of all contacts in the phone. When you select on of the names in the list, you get the name and number, but not the email address. The toast message between this code never shows up.
[HIGH]
while(emails.moveToNext()){
contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
}
[/HIGH]
I'm very confused why this isn't working and would really preciate if someone could tell me what I have missed or done wrong? Thanks!
[HIGH]public class Activity_3 extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
listView = (ListView) findViewById(R.id.contactList);
String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID };
Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
// From column
String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
// To view
int[] toView = { R.id.contactItem };
startManagingCursor(cursor1);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,
};
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
cursor.moveToPosition(position);
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone._ID));
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String pos = Integer.toString(position);
String contactEmailAddress = "?";
//Email
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null);
while(emails.moveToNext()){
contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
}
emails.close();
Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();
}
});
}
}[/HIGH]
In my application I have a listview of all contacts in the phone. When you select on of the names in the list, you get the name and number, but not the email address. The toast message between this code never shows up.
[HIGH]
while(emails.moveToNext()){
contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
}
[/HIGH]
I'm very confused why this isn't working and would really preciate if someone could tell me what I have missed or done wrong? Thanks!
[HIGH]public class Activity_3 extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
listView = (ListView) findViewById(R.id.contactList);
String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID };
Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
// From column
String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
// To view
int[] toView = { R.id.contactItem };
startManagingCursor(cursor1);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,
};
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
cursor.moveToPosition(position);
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone._ID));
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contactNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String pos = Integer.toString(position);
String contactEmailAddress = "?";
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null, null);
while(emails.moveToNext()){
contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
}
emails.close();
Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();
}
});
}
}[/HIGH]