我是在互联网的帮助下自己制作的,并参考了几本书。下面的代码提取特定联系人的姓名和电话号码,并且可以正常工作(甜蜜)。我的问题是关于 selection 和 selectionArgs。如何添加电子邮件查询?
我不理解所有的等号和 AND withing 引号,所以如果你能指出我可以在哪里了解更多关于“选择”中使用的语言,而不是仅仅用代码给我答案,那也有帮助我非常。
提前致谢
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);
}
}
编辑:我尝试了 Sandy09 的想法(减去我认为 cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) 之后的错字);
现在我得到了这个错误,当我重新复制我的代码时,我仍然得到它(减去线程引用)。感谢所有帮助:
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)