Hello All :)
I am trying to create a ListView that can contain different types of
animals (Cat, Dog) that all extend the abstract class Animal.
I understand that the most efficient way to deal with ListViews is to
use a custom ViewHolder to save the views attribute.
Depending on the type of the animal to display, the XML layout used is
different so I created the classes ViewHolderCat, ViewHolderDog that
extend the abstract class ViewHolderAnimal.
Then, I wrote a custom AnimalAdapter that extends BaseAdapter to
return the correct view.
Here is the getView method:
public View getView(int position, View convertView, ViewGroup parent)
{
Animal a = getItem(position);
ViewHolderAnimal holder;
if(convertView == null){
convertView = a.inflateView(this.inflater); // inflates
the correct
XML, depending on the class of object "a"
switch(getItemViewType(position)){
case TYPE_DOG:
holder = new ViewHolderDog(convertView); //
attaches the holder
attributes to the XML entities
break;
case TYPE_CAT:
holder = new ViewHolderCat(convertView); //
attaches the holder
attributes to the XML entities
break;
default:
holder = null;
break;
}
convertView.setTag(holder);
}else{
holder = (ViewHolderAnimal) convertView.getTag();
}
holder.setFields(a); // Set the text and images of the XML
entities
return convertView;
}
The application is working just fine so far, but I feel like it might
not be done the cleanest way.
For the example, it looks weird to me that the inflateView function is
in the Animals class, but I don't really know where it should be.
The main purpose of this application is to learn, so I would really
like to learn the cleanest way to do this.
Do not hesitate to ask if you need me to post other parts of the code
or to answer any other question.
Thanks a lot.
Nathan.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en