I'm making this on my own with help from the interwebs and a lot of reference to a few books. The code below pulls a specific contact's name and phone number and it works (sweet). My question is about the selection and selectionArgs. How do I add in a query for email?
I don't understand all the equal signs and the AND withing quotations, so if you can point me to where I can learn more about the language used in 'selection' rather than just giving me the answer in code, that'd also help me tremendously.
Thanks in advance
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
/**
* Created by Pete on 6/19/13.
*/
public class ContactDetails extends Activity {
TextView tvContactName, tvPhoneNum, tvPhoneType, tvPhoneFull,
tvEmailAdd, tvEmailType, tvEmailFull,
tvAddress, tvAddType, tvAddFull;
String contactId, contactName, phoneType, phoneFull, phoneNum1,
emailAdd, emailType, emailFull,
address, addType, addFull;
//Contact List query arguments
Uri uri;
String[] projection, selectionArgs;
String selection, sortOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactinfo);
initialize();
contactId = getIntent().getStringExtra("contactId");
contactName = getIntent().getStringExtra("contactName");
queryArgs();
// Create cursor searching for data associated with contactId
if (contactId != null) {
// Return all the PHONE data for the contact
Cursor cursor = getContentResolver().query(
uri, projection, selection, selectionArgs, sortOrder);
//Get the indexes of the required columns
while (cursor.moveToNext()) {
// Extract the name
contactName = cursor.getString(
cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
tvContactName.setText(contactName);
// Extract the phone number
phoneFull = cursor.getString(
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
tvPhoneFull.setText(phoneFull);
}
cursor.close();
}
}
public void initialize() {
tvContactName = (TextView)findViewById(R.id.tvContactName);
tvPhoneNum = (TextView)findViewById(R.id.tvPhoneNum);
tvPhoneType = (TextView)findViewById(R.id.tvPhoneType);
tvPhoneFull = (TextView)findViewById(R.id.tvPhoneFull);
tvEmailAdd = (TextView)findViewById(R.id.tvEmailAdd);
tvEmailType = (TextView)findViewById(R.id.tvEmailType);
tvEmailFull = (TextView)findViewById(R.id.tvEmailFull);
tvAddress = (TextView)findViewById(R.id.tvAddress);
tvAddType = (TextView)findViewById(R.id.tvAddType);
tvAddFull = (TextView)findViewById(R.id.tvAddFull);
}
public void queryArgs() {
// Cursor query arguments
uri = ContactsContract.Data.CONTENT_URI;
projection = new String[] {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
selection = ContactsContract.Data.CONTACT_ID + " = " + contactId + " AND " +
ContactsContract.Data.MIMETYPE + " = " +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
selectionArgs = null;;
sortOrder = null;
}
public void setTextValues() {
tvContactName.setText(contactName);
tvPhoneNum.setText(phoneNum1);
tvPhoneType.setText(phoneType);
tvPhoneFull.setText(phoneFull);
tvEmailAdd.setText(emailAdd);
tvEmailType.setText(emailType);
tvEmailFull.setText(emailFull);
tvAddress.setText(address);
tvAddType.setText(addType);
tvAddFull.setText(addFull);
}
}
edit: I tried out Sandy09's idea (minus what I believe is a typo after cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Now I'm getting this error, and when I recopy my code, I get it still (minus the thread reference). All help is appreciated:
06-22 04:45:56.316: E/AndroidRuntime(8818): FATAL EXCEPTION: Thread-273
06-22 04:45:56.316: E/AndroidRuntime(8818): android.database.sqlite.SQLiteException: unrecognized token: "'vnd.android.cursor.item/phone_v2))" (code 1): , while compiling: SELECT DISTINCT display_name, data1 FROM view_data data WHERE (1) AND ((contact_id = 1 AND mimetype = 'vnd.android.cursor.item/phone_v2))
06-22 04:45:56.316: E/AndroidRuntime(8818): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
06-22 04:45:56.316: E/AndroidRuntime(8818): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
06-22 04:45:56.316: E/AndroidRuntime(8818): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
06-22 04:45:56.316: E/AndroidRuntime(8818): at android.content.ContentResolver.query(ContentResolver.java:372)
06-22 04:45:56.316: E/AndroidRuntime(8818): at android.content.ContentResolver.query(ContentResolver.java:315)
06-22 04:45:56.316: E/AndroidRuntime(8818): at sat.tuts4mobile.customlistview.ContactDetails$1.run(ContactDetails.java:42)
06-22 04:45:56.316: E/AndroidRuntime(8818): at java.lang.Thread.run(Thread.java:856)