如何在onBindViewHolder中为列表中的每一行设置图像?

时间:2016-06-22 09:49:55

标签: java android image list android-recyclerview

我有受邀者名单。我正在使用recyclerview和自定义适配器。在这里,我通过contactId从联系人那里获取照片。

现在我将imageUri设置为图像视图。对于imageUri不为null的所有行,只显示一个图像。当我向上和向下滚动时,位置会发生变化。有时它显示图像,有时它不显示。

我想根据被邀请者的位置设置uri。怎么做?

适配器:

    public class InviteeAdapter extends RecyclerView.Adapter<InviteeAdapter.MyViewHolder>{


    private List<Invitee> inviteeList;
    int status;
    Context context;
    Cursor mCursor;

    private ArrayList<Uri> imageArray;
    public String contactId;


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public com.github.siyamed.shapeimageview.CircularImageView profileImage;

        String mobileNo;


        public MyViewHolder(View view) {

            super(view);
            name = (TextView) view.findViewById(R.id.scheduleName);
            profileImage = (com.github.siyamed.shapeimageview.CircularImageView) view.findViewById(R.id.eventsIcon);

            imageArray = new ArrayList<>();
        }

        public String fetchContactIdFromPhoneNumber(String phoneNumber) {
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(phoneNumber));
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},
                    null, null, null);

            String contactId = "";

            if (cursor.moveToFirst()) {
                do {
                    contactId = cursor.getString(cursor
                            .getColumnIndex(ContactsContract.PhoneLookup._ID));
                } while (cursor.moveToNext());
            }

            return contactId;
        }

    }

    public InviteeAdapter(List<Invitee> inviteeList,Context context) {
        this.inviteeList = inviteeList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.invitee_card, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        Uri imageUri;

        Invitee invitee = new Invitee();

        invitee = inviteeList.get(position);
        holder.name.setText(invitee.getFName()+" "+ invitee.getLName());

        contactId = holder.fetchContactIdFromPhoneNumber(invitee.getMobile());

        ArrayList<String> contactArray = new ArrayList<>();

        imageUri = getPhotoUri();

        imageArray.add(getPhotoUri());

        contactArray.add(contactId);

        for(Uri  id : imageArray)
        {
            //  imageUri = getPhotoUri();

            if(imageUri!= null) {

                holder.profileImage.setImageURI(id);
            }
            else {

                holder.profileImage.setBackgroundResource(R.drawable.ic_person_black_48dp);

            }

        }

       /* status = invitee.;

        if(status == 0)
        {
            holder.name.setTextColor(context.getResources().getColor(R.color.colorAccent));

        }

        else {

            holder.name.setTextColor(context.getResources().getColor(R.color.grey));
        }*/


    }


    public Uri getPhotoUri() {
        try {
            Cursor cur = context.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID + "=" + contactId  + " AND "
                            + ContactsContract.Data.MIMETYPE + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                    null);
            if (cur != null) {
                if (!cur.moveToFirst()) {
                    return null; // no photo
                }
            } else {
                return null; // error in cursor process
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
                .parseLong(contactId));
        return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }

    @Override
    public int getItemCount() {
        return inviteeList.size();
    }

}

有人可以帮忙吗。谢谢..

1 个答案:

答案 0 :(得分:0)