0

Again, I've been trying to find answers all over the net. However I can't seem to find one to match my issue.

Basically, I have a list of emoticons from an API I'm using and I'm trying to get these images to be displayed in a TextView which just isn't working. The images appear as blue squares in my recycle view until I scroll back up to them then they appear. Here is my code and I'm not sure what I'm doing is correct. I can't remember why I did what I did as It's old code and I'm trying to refactor it. Can anyone help?

Here's the code:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Posts posts = mPost.getItem(position);
    emoticons = mEmoticon.getItems();
    String message = null;
    String emoMessage = null;

    if (posts.getPost() != null) {
        if (posts.getPost().getMessage() != null) {
            message = posts.getPost().getMessage();
            emoMessage = message;


            if (emoticons != null) {
                for (Emoticons emoticon : this.emoticons) {
                    if (message.contains(emoticon.getEmoticon().getCode())) {
                        emoMessage = message.replaceAll(Constants.EMO_REGEX, emoticon.getEmoticon().getUrl());
                    }

                }
            }

holder.mPostTextView.setText(Html.fromHtml(emoMessage, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(final String source) {
                Target loadTarget;
                loadTarget = new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        try {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                            URL url = new URL(source);
                            InputStream is = url.openStream();
                            Bitmap b = BitmapFactory.decodeStream(is);
                            mDrawable = new BitmapDrawable(Resources.getSystem(), b);
                            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth() + 25, mDrawable.getIntrinsicHeight() + 25);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                };
                Picasso.with(mContext).load(source).into(loadTarget);
                return mDrawable;
            }
        }, null);
}
BilalMH
  • 175
  • 1
  • 21

1 Answers1

0

It seems that when you call mPostTextView.setText() your mDrawable has not yet been initialised. Later, when onBitmapLoaded() is called you are then updating mDrawable to point to youw newly created BitmapDrawable. But this won't update the TextView.

I would try to rearrange things so that inside onBindViewHolder() you call

Picasso.with(mContext).load(source).into(loadTarget);

and then at the end of onBitmapLoaded() call mPostTextView.setText(). In case you need to keep a reference to emoMessage in the meantime, you could pass it as a field in your Target.

danwilkie
  • 794
  • 6
  • 10
  • But I'm doing all of this in my setText() how would I go about changing that? – BilalMH Feb 16 '16 at 14:46
  • Sorry - I had another look and I don't think my idea will work. This may help? http://stackoverflow.com/questions/25194549/using-picasso-with-image-getter – danwilkie Feb 18 '16 at 09:31