0

I have to write an application to parse a list of nodes from an online XML file (each node contains a text, a link to an image, and a link to a website), and display them in a ListView so that selecting an element of the ListView (with the corresponding image and title) will open the related link in the browser. At the moment I have parsed the information in the XML file, and I have it stored in three String arrays for easy access/handling: titleArray, imageurlArray and linkArray

I have tried to modify Fedor's Lazy Loading application ( https://stackoverflow.com/a/3068012/1114109 ) in order to make it work with the titleArray and the imageurlArray, but I just don't seem to be able to get it right.

Community
  • 1
  • 1
antak
  • 29
  • 1

1 Answers1

2

I'd say the easiest approach would be to bundle your three separate arrays into one array of objects containing the same data. In other words, create a entity object with three attributes: title, imageUrl and link.

class XMLEntity {
    String title, imageUrl, link;
    // add contructor, getters and setters to your own liking...
}

...
XMLEntity[] objects = ...

You then pass this array of objects to adapter's constructor and in the getView method you can set the TextView, ImageView and other UI elements based on the data in the XMLEntity.

public LazyAdapter(Activity a, XMLEntity[] objects) {
    //...
}

public View getView(int position, View convertView, ViewGroup parent) {
    // ... convertview stuff
    XMLEntity current = objects[position]
    someTextView.setText(current.getTitle());
    imageLoader.DisplayImage(current.getImageUrl(), someImageView);
}

If you're going down this path, I'd also have a look at ArrayAdapter, which simplifies some of the BaseAdapter's logic for you.

//Edit: If you like a quick 'n dirty solution: move the implementation of the adapter as a subclass to the activity containing your ListView and simply have it work on the three arrays you currently have - assuming that's where they are stored and the indices 'match' for all three arrays.

public View getView(int position, View convertView, ViewGroup parent) {
    // ... convertview stuff
    someTextView.setText(titleArray[position); // get title from titleArray
    imageLoader.DisplayImage(imageurlArray[position], someImageView); //get image url from imageurlArray
}
MH.
  • 45,303
  • 10
  • 103
  • 116
  • If my answer satisfies your question, please consider it marking as 'answer' for future reference. – MH. Dec 24 '11 at 20:22