如何从联系人处获取名字和姓氏?

时间:2016-06-15 09:43:01

标签: java android android-contacts

我想从联系人那里获得联系人的名字和姓氏。我有一个查询,但它抛出异常。

我想获得联系人的名字,姓氏,电子邮件ID和手机号码。 我怎么能得到这个?

代码:

 private void getContacts() {


    String[] projection = new String[] {
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
            ContactsContract.Contacts._ID };

    mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME + ") ASC");

    while (mCursor.moveToNext()) {
        Organizers organizers = new Organizers();

        String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));

        String givenName = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));

        String lastName = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

        organizers.setId(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID)));
        organizers.setFName(givenName);
        organizers.setLName(lastName);
        contact_list.add(organizers);
    }
    isChecked = new boolean[mCursor.getCount()];

    for (int i = 0; i < isChecked.length; i++) {
        isChecked[i] = false;
    }

    this.mContactAdapter = new contactAdapter(this, R.layout.contact_list_item, contact_list);
    lv.setAdapter(this.mContactAdapter);
}

记录:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.siddhi.meavita/com.example.siddhi.meavita.Activities.ContactList}: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: SELECT sort_key, photo_uri, status_updates.status_label AS status_label, status_updates.status_ts AS status_ts, status_updates.status_res_package AS status_res_package, display_name, data_usage_stat.last_time_used AS last_time_used, mimetype, phonebook_label_alt, data6, version, photo_id, data3, custom_ringtone, times_contacted, account_type_and_data_set, dirty, data7, data15, raw_contact_is_user_profile, data_set, phonebook_label, data10, res_package, account_type, data11, display_name_alt, lookup, phonetic_name, last_time_contacted, contact_last_updated_timestamp, data13, in_visible_group, presence.chat_capability AS chat_capability, data9, carrier_presence, data_sync1, sort_key_alt, agg_presence.mode AS contact_presence, data_version, phonetic_name_style, name_raw_contact_id, raw_contact_id, send_to_voicemail, data4, data12, contacts_status_updates.status AS contact_status, contacts_status_updates.status_label AS contact_status_label, pinned, status_updates.status_icon AS status_icon, status_updates.status AS status, data1, phonebook_bucket, data_sync2, contacts_status_updates.status_res_package AS contact_status_res_package, in_default_directory, _id, hash_id, is_super_primary, data5, contact_id, data8, is_primary, data_sync4, has_phone_number, display_name_source, photo_file_id, data_sync3, backup_id, data14, contacts_status_updates.status_ts AS contact_status_ts, phonebook_bucket_alt, presence.mode AS mode, data2, group_sourceid, starred, photo_thumb_uri, data_usage_stat.times_used AS times_used, contacts_status_updates.status_icon AS contact_status_icon, agg_presence.chat_capability AS contact_chat_capability, account_name, sourceid FROM view_data data LEFT OUTER JOIN agg_presence ON (contact_id = agg_presence.presence_contact_id) LEFT OUTER JOIN status_updates contacts_status_updates ON (status_update_id=contacts_status_updates.status_update_data_id) LEFT OUTER JOIN presence ON (presence_data_id=data._id) LEFT OUTER JOIN status_updates ON (status_updates.status_update_data_id=data._id) LEFT OUTER JOIN (SELECT data_usage_stat.data_id as STAT_DATA_ID, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_usage_stat.data_id) as data_usage_stat ON (STAT_DATA_ID=data._id) WHERE (1) ORDER BY data2) ASC
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: SELECT sort_key, photo_uri, status_updates.status_label AS status_label, status_updates.status_ts AS status_ts, status_updates.status_res_package AS status_res_package, display_name, data_usage_stat.last_time_used AS last_time_used, mimetype, phonebook_label_alt, data6, version, photo_id, data3, custom_ringtone, times_contacted, account_type_and_data_set, dirty, data7, data15, raw_contact_is_user_profile, data_set, phonebook_label, data10, res_package, account_type, data11, display_name_alt, lookup, phonetic_name, last_time_contacted, contact_last_updated_timestamp, data13, in_visible_group, presence.chat_capability AS chat_capability, data9, carrier_presence

请帮忙。谢谢。

0 个答案:

没有答案