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);
}