views:

33

answers:

1

I've made a customAdapter that accepts an ArrayList. The ArrayList contains a title and then a link.

For example: [Title1, http://mylink1.com/, Title2, -http://mylink2.com/-, Title3, -http://mylink3.com/- ...] ** I've put hypens there because of the "Stackoverflow" Spam filter.

I'm wanting to display the title and then have the on click listener to have the link. I'm having trouble however figuring out a way to do this. Any help would be greatly appreciated! :)

+1  A: 

The ArrayList needs to contain Link objects.

Ex.

class Link {
    String title;
    String url;
}

In getView of your Adapter you would use the title to fill the TextView, and in the onClick on onSelect or whatever, you would have the Link object with the title and url.

Ex.

public View getView(int index, View convertView, ViewGroup parent) {
    TextView tv = (TextView) convertView;
    Link link = list.get(index);
    tv.setText(link.getTitle());
    return tv;
}

public void onItemClick(ViewGroup parent, View view, int position, long id) {
    Link link = list.get(position);
    String uri = link.getUri();
    // do something interesting.
}

And if your Adapter extends ArrayAdapter and you aren't overriding getView(), then the toString() method of Link needs to return the title field.

BrennaSoft
You can also use a SimpleAdapter to avoid having to manually implement the getView function. See http://www.vbsteven.be/blog/using-the-simpleadapter-with-a-listview-in-android/ for an example.
Mayra
fwaokda