4

List view clickable link poblem.

I'm using the following code inside the getView() to generate a clickable link in a listview.

myTextView.setMovementMethod(LinkMovementMethod.getInstance());
String linkText = "<a href=\"http://www.google.com\">Google</a>";
myTextView.setText(Html.fromHtml(linkText));

This code works fine on a textview which is not in a listview but when i use it for a textview within a list view the following exception is raised on clicking the link.

AndroidRuntimeException: Calling startActivity() from outside of an Activity
context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Vikas Gulati
  • 968
  • 2
  • 10
  • 24
  • Something sounds weird to me.. this code is inside a custom adapter right? is there an onClickListener in it? – Joe Apr 23 '12 at 10:13
  • yeah this is inside the custom adapters getView method. No there isn't and onClickListner to it. – Vikas Gulati Apr 23 '12 at 10:17
  • umm.. I found this http://www.stevenmarkford.com/solution-to-android-error-calling-startactivity-outside-activity-context-requires-flag_activity_new_task/ dont know if it will help – Joe Apr 23 '12 at 10:26
  • thanks but i found a simpler fix. will post in sometime. – Vikas Gulati Apr 23 '12 at 10:47

3 Answers3

8

Got the answer here. I just had to change the constructor call from

CustomAdapter mAdapter = new CustomAdapter( mContext, itemList);

to

CustomAdapter mAdapter = new CustomAdapter( this, itemList);
Community
  • 1
  • 1
Vikas Gulati
  • 968
  • 2
  • 10
  • 24
  • 1
    Great Answer, for those who struggle to solve. Simply, from where you are initialising your `Adapter` be it any adapter `CustomAdapter`, `ArrayAdapter` or your regular `BaseAdapter`, just put `this` instead of `getApplicationContext()` or something else – Anas Azeem Jun 13 '14 at 13:11
  • 1
    Thank you soooo much, was going crazy trying to figure out why i was getting this error :) – cking24343 Mar 06 '15 at 19:44
0
   TextView textView2 = (TextView)findViewById( R.id.TextView2 );
   SpannableStringBuilder ssb = new SpannableStringBuilder( http://google.com" );
   textView2.setText( ssb, BufferType.SPANNABLE );
   Linkify.addLinks( textView2, Linkify.WEB_URLS );

     Try this one....
Sock
  • 418
  • 1
  • 5
  • 12
0

I had the problem that the ApplicationContext was used for inflating the views. Changed it to ActivityContext, now it works!

stoefln
  • 14,498
  • 18
  • 79
  • 138