views:

48

answers:

1

I have a working android app using TextView, some formatting (line breaks, rows of dashes) and Linkify to generate a primitive "ListView-like" display with clickable URLs in each "row". I'd like to move up to a real ListView, but I'm just not finding the sample/explanation that I need to take that next step.

I have successfully reproduced the HelloListView sample, starting with the hardcoded string array, and moving to a string array defined in my res/values/strings.xml. I've taken one small step toward my goal by adding my HttpClient code to retrieve a set of data from a service, parse the results into a String Array and feed that into setListAdapter() such that my text and links show up as text-only in ListView items.

I want to move to the next step which is to make each "row" in my ListView launch the browser to the URL contained in the data, either by (A) clicking anywhere in the row, or (B) clicking a hyperlink displayed within the row data

For option (A), it appears that I need to have my onItemClick() method issue an intent that launches the browser. That's straightforward, but I don't get how to associate the URL with the item (currently its just one part of the string content for each "row" of text). How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL? Do I need to replace my String Array with an array of custom objects?

For option (B), can I use Linkify? It seems that my string array elements get converted to individual TextViews (inferring from the way the Toast text is generated in the HelloListView sample). Do I have access to that TextView to run Linkify against? Do I need to replace my String Array with a TextView Array and run Linkify myself? Am I completely off base?

Thanks to anyone who can help explain back to me what I'm trying to do, in a way that helps to find my way around the SDK, samples and other helps!

A: 

How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL?

Use a regular expression (java.util.regex) to find the URL.

For option (B), can I use Linkify?

Yes.

Do I have access to that TextView to run Linkify against?

Yes. Override getView() in your ArrayAdapter. Chain to the superclass and get your TextView from the result of super.getView().

Even better would be to use Linkify on your strings before putting them in the array in the first place.

Do I need to replace my String Array with a TextView Array and run Linkify myself?

No, and that is really not a good idea. Here is a free excerpt from one of my books that goes into more detail on tailoring the individual rows of a ListView, in case this helps.

CommonsWare
Thanks much, especially for the excerpt!
jeffonc
**regex suggestion is doable in my first example - maybe not as extensible for other cases where there may be multiple links or you may not want to display the link in the ListView. **I did not realize I could Linkify a string, stuff it into a string array, stuff that into a ListView, and retain the Linkify-cation at display time. Will have to play with that. **Excerpt is exactly the kind of conceptual description I was looking for. Thanks again!
jeffonc