Linkify inconsistency

234 views Asked by At

I have a list with photos like Instagram, with text that has links to hashtags and users using # and @ and I'm using Linkify to make this work.

The 1st item in my list gets the links correct 8/10 times but the other times its just plain text. The other items always get their links correct.

BaseAdapter

// holder is a ViewHolder class holding my row views
// holder.photo is a ParseFile subclass object 
// holder.photo.hashtags is an arraylist with objects
// holder.photo.comment() is a getString("key") from ParseObject
// hashtags is a String with hashtags, for example "#hashtag #anotherhashtag"

holder.hashtag.setText(hashtags, TextView.BufferType.SPANNABLE);
holder.title.setText(holder.photo.comment(), TextView.BufferType.SPANNABLE);
StringUtils.linkify(activity, holder.photo.hashtags, new TextView[]{ holder.hashtag, holder.title });

StringUtils

public static void linkify(Activity activity, List<Hashtag> hashtags, TextView[] textViews ) {
    for (TextView t : textViews) {

        // currently only using hashtags
        Pattern tagMatcher = Pattern.compile("[#]+[A-Za-z0-9-_]+\\b");

        // Linkify url
        String newActivityURL = "app://";

        // Add links
        Linkify.addLinks(t, tagMatcher, newActivityURL);

        // Get spannable
        Spannable spannable = (Spannable) t.getText();
        URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);

        // Replace text with span
        for (URLSpan span : spans) {
            int start = spannable.getSpanStart(span);
            int end = spannable.getSpanEnd(span);
            spannable.removeSpan(span);

            // Hashtag is a ParseObject subclass
            Hashtag selectedHashtag = null;
                for (Hashtag h : hashtags) {
                    // Fix url for clickable link
                    String URL = span.getURL().replace("app://" + "#", "");
                    if (URL.equals(h.getObjectId())) {
                        selectedHashtag = h;
                    }
                }

                if (selectedHashtag != null) {
                    span = new URLSpanNoUnderline(a, selectedHashtag, selectedHashtag.name());
                    spannable.setSpan(span, start, end, 10);
                }
            }
        }

URLSpanNoUnderline

class URLSpanNoUnderline extends HashtagSpan {

    private Activity a;

    public URLSpanNoUnderline(Activity a, Hashtag h, String p_Url) {
        super(p_Url, h);
        this.a = a;
    }

    @Override
    public void onClick(View v) {
        HashtagActivity.hashtag = hashtag;
        // Start activity
    }


    public void updateDrawState(TextPaint p_DrawState) {
        super.updateDrawState(p_DrawState);
        p_DrawState.setUnderlineText(false);
        p_DrawState.setFakeBoldText(true);
    }
}
1

There are 1 answers

0
Jonas Borggren On BEST ANSWER

I have solved it.

The problem came from all threads not finishing simulatenously. I had one task where it would get information about my picture and one that would get the hashtags (from relation to actual object) that were attached to the photo.

Solution was to make sure all info was loaded into the adapter when the last object in my for-loop was added to the list of objects making sure there were no objects added/changed after the adapter had been updated.