Android Contact Picker With Checkbox

大兔子大兔子 提交于 2019-11-27 21:39:08
Nam Trung

I use this code in onClick:

long[] id = getListView().getCheckedItemIds();//  i get the checked contact_id instead of position
        phoneNumber = new String[id.length];
        for (int i = 0; i < id.length; i++) {

            phoneNumber[i] = getPhoneNumber(id[i]); // get phonenumber from selected id

        }

        Intent pickContactIntent = new Intent();
        pickContactIntent.putExtra("PICK_CONTACT", phoneNumber);// Add checked phonenumber in intent and finish current activity.
        setResult(RESULT_OK, pickContactIntent);
        finish();

//

private String getPhoneNumber(long id) {
    String phone = null;
    Cursor phonesCursor = null;
    phonesCursor = queryPhoneNumbers(id);
    if (phonesCursor == null || phonesCursor.getCount() == 0) {
        // No valid number
        signalError();
        return null;
    } else if (phonesCursor.getCount() == 1) {
        // only one number, call it.
        phone = phonesCursor.getString(phonesCursor
                .getColumnIndex(Phone.NUMBER));
    } else {
        phonesCursor.moveToPosition(-1);
        while (phonesCursor.moveToNext()) {

            // Found super primary, call it.
            phone = phonesCursor.getString(phonesCursor
                    .getColumnIndex(Phone.NUMBER));
            break;

        }
    }

    return phone;
}


private Cursor queryPhoneNumbers(long contactId) {
    ContentResolver cr = getContentResolver();
    Uri baseUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
            contactId);
    Uri dataUri = Uri.withAppendedPath(baseUri,
            ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

    Cursor c = cr.query(dataUri, new String[] { Phone._ID, Phone.NUMBER,
            Phone.IS_SUPER_PRIMARY, RawContacts.ACCOUNT_TYPE, Phone.TYPE,
            Phone.LABEL }, Data.MIMETYPE + "=?",
            new String[] { Phone.CONTENT_ITEM_TYPE }, null);
    if (c != null && c.moveToFirst()) {
        return c;
    }
    return null;
}

And the last onActivityResult of activity which you start PickContactsActivity

    // TODO Auto-generated method stub
    // super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {

        if (requestCode == Constants.REQUEST_CODE_PICK_CONTACT) {


            if (data != null) {

                String[] temp = data.getStringArrayExtra("PICK_CONTACT");

            }
        }

    }

}

When using startActivityForResult(newActivity) in the newActivity you must make a call to setResult(RESULT_OK) followed by finish() to close the Activity. Optionally you can include an Intent in the call to setResult(RESULT_OK, intent). The call to setResult() will lead to calling your implementation of onActivityResult() where you can handle the result of the Activity. So in your case you would just create an Intenet and add your array list to it using one of the putExtra() methods. That Intent will then be passed to onActivityResult() where you can extract that information. See Intent and Activity for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!