0

I have a textview created in a class B programaticly. When class A calls it.

It adds text say "I went to the shop" and i need the word "the" to be a link. This i have done using this in CLASS B

sb.setSpan(new MyClickableSpan(Word), start, start+Word.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

which is calling the class MyClickableSpan

public class MyClickableSpan extends ClickableSpan {
private String word;

public MyClickableSpan(String word) {
    this.word = word;
}

@Override
public void onClick(View widget) {

    Log.d("Spaannnned",word);

}
}

So when this link is clicked it is showing up in my log.

But I need it to run a function in Class A and pass in the word.

Please help

RuAware
  • 979
  • 1
  • 9
  • 26

2 Answers2

0

Supply your instance of "Class A" to the constructor of MyClickableSpan, save it in a data member, and use that from your onClick() method.

Or, use an event bus like Otto, to have onClick() send out a message that "Class A" subscribes to.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Otto looks interesting.. will look tomorrow. Unfortunately cllass b where the span is set. Does not know about class a. – RuAware Apr 15 '13 at 22:38
0

Just add a click listener interface to your span:

public class MyClickableSpan extends ClickableSpan {
public interface SpanClickListener
{
     public void onSpanClicked(View widget, MyClickableSpan span);
}
private String word;
private SpanClickListener clickListener;

public MyClickableSpan(String word, SpanClickListener listener) {
    this.word = word;
    clickListener = listener;
}
public String getWord() { return word; }

@Override
public void onClick(View widget) {
        Log.d("Spaannnned",word);
        if (clickListener != null) clickListener.onSpanClicked(widget, this);
    }
}


Then, in your activity:

MyClickableSpan span = new MyClickableSpan("Hello!", new MyClickableSpan.SpanClickListener() {
           @Override
           public void onSpanClicked(View widget, MyClickableSpan span)
           {
                Toast.makeText(context, span.getWord(), Toast.LENGTH_SHORT);
           }
        };
....
//add your span to the Spannable or whatever you want.
Const
  • 976
  • 7
  • 11
  • But wouldnt with this method have to set the span in class a? Whic at the moment isnt an ooption – RuAware Apr 15 '13 at 22:36
  • Well, if you want to call something in class A, then you must somehow supply a reference to instance of A to class B anyway. Unless you are using a message bus or message queue of some sort, which I don't think is the case. – Const Apr 15 '13 at 22:45
  • Just create an instance of SpanClickListener in A, then supply it to the class B, which will use it during creation of spans. – Const Apr 15 '13 at 22:50
  • Thank you, i got most of it using your idea. As class B creating the span I also got it to save them in an array list. Then in class A I use a for loop telling it the activity. Then implement mySpanListener and it seems to work. Thanks for your help – RuAware Apr 16 '13 at 18:46