【发布时间】:2016-08-17 12:19:57
【问题描述】:
我正在尝试一个项目,我需要电话号码中包含 123 的电话的所有联系人。我可以检索联系人,但 contentresolver 中的“where”子句不起作用这是我的参考代码
public void fetchContacts() {
String phoneNumber = null;
String email = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
// Loop for every contact in the phone
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0) {
output.append("\n First Name:" + name);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, NUMBER + " = 123", null, null);
while (phoneCursor.moveToNext())
{
phoneNumber = cursor.getString(phoneCursor.getColumnIndex(NUMBER));
//
output.append("\n Phone number:" + phoneNumber);
}
phoneCursor.close();
}
output.append("\n");
}
outputText.setText(output);
}
}
}
LOGCAT java.lang.RuntimeException:无法启动活动 ComponentInfo{com.gamemyworldgame.contact/com.gamemyworldgame.contact.MainActivity}: 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 在 android.app.ActivityThread.access$800(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5257) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 原因:android.database.sqlite.SQLiteException:没有这样的列:phone.NUMBER(代码1):,
【问题讨论】:
标签: android android-sqlite contactscontract