简体   繁体   中英

android quickcontactbadge not showing?

I'm trying to add a QuickContactBadge to a Recyclerview .

Details:

I have a list where each item has a photo of a contact and some text.

What i want:

I would like to click on the image and bring up the QuickContactBadge. but it's not work for me.

Here's my code:

public class CoViewHolder extends RecyclerView.ViewHolder {
    QuickContactBadge imageView;
    TextView title, phone;
    ImageView call;
    String pho, nam;
    int position;
    boolean concal;
    String contactID;
    private static final int CONTACT_PICKER_RESULT = 0;

    public CoViewHolder(View v, int viewType) {
        super(v);
        title = (TextView) v.findViewById(R.id.name);
        phone = (TextView) v.findViewById(R.id.no);
        call = (ImageView) v.findViewById(R.id.ccbtn);
        imageView = (QuickContactBadge) v.findViewById(R.id.pic);


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ii = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(ii, CONTACT_PICKER_RESULT);
            }
        });
    }

    private void startActivityForResult(Intent ii, int contactPickerResult) {
        if(contactPickerResult==RESULT_OK)
        switch (contactPickerResult) {
            case CONTACT_PICKER_RESULT:
                Uri contactUri =  ii.getData();
               FrameLayout frameLayout = (FrameLayout)activity.findViewById(R.id.badge_holder_large);
                QuickContactBadge badge = new QuickContactBadge(activity);
                badge.assignContactUri(contactUri);
                badge.setMode(ContactsContract.QuickContact.MODE_LARGE);
                frameLayout.addView(badge);


                Cursor cursorID = activity.getContentResolver().query(contactUri,
                        new String[]{ContactsContract.Contacts._ID}, null, null, null);
                if (cursorID.moveToNext()) {
                    contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));

                }

                InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(activity.getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));

                BufferedInputStream buf =new BufferedInputStream(input);
                Bitmap my_btmp = BitmapFactory.decodeStream(buf);

                if(my_btmp != null)
                    badge.setImageBitmap(my_btmp);
                else
                    badge.setImageResource(R.drawable.image);

        }
    }
}

I never figured out how to make assignContactUri() to work properly. Anyway, since you're working with ContactsContract.Contacts , I guess you could bind the contact badge by phone (IE, when you have it), with something like:

badgeMedium.assignContactFromPhone("666-555-1234", true);

instead of:

badge.assignContactUri(contactUri);

In order to retrieve contact's phone number you can have a look here , but you already have much of the relevant code. Also this old link could help, as QuickContactBadge API hasn't changed much since then.

Finally please note that according to docs the badge.setMode() call has no effect anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM