Can you please help me understand the adapter with custom data :
I was reading a tutorial that explained that to fill a listView with a model like MyBook (with 2 variables : author and title for example), we should create a subclass of BaseAdapter and override those methods :
Here is the code :
public LivreAdapter(Context context,List<Livre> biblio) {
inflater = LayoutInflater.from(context);
this.biblio = biblio;
}
@Override
public int getCount() {
return biblio.size();
}
@Override
public Object getItem(int position) {
return biblio.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView tvTitre;
TextView tvAuteur;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.itemlivre, null);
holder.tvTitre = (TextView)convertView.findViewById(R.id.tvTitre);
holder.tvAuteur = (TextView)convertView.findViewById(R.id.tvAuteur);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitre.setText(biblio.get(position).getTitre());
holder.tvAuteur.setText(biblio.get(position).getAuteur());
return convertView;
}
}
-
Do we create a subclass of BaseAdapter only to display the custom data the way we want (with subtitles etc.)? Because it’s really simple to fill a listView with a String array, here we need to subclass the BaseAdapter…
-
I cannot find those methods in the doc, so getItem will be called before getView? at the end of the method, we set “holder” to hold the text from the database, but I don’t see where we re-use this “holder”, since the only variable returned is “convertView”?
I’m looking for infos in the doc but can’t find something that help me understand exactly the process of the class.
There are other Classes that also can be subclassed.Simple listView is useful but it cannot be used to display custom data.In order to be able to fill listView with custom data you need to use a adapter that inflates each row of listView. getItem may be used from within getView inorder to get specific entry from the list.When Adapter creates View these views are recycled so ViewHolders may be used