I’m using different templates in a listview that is filled from an array through a custom adapter.
I’m adding items to a List ‘items’, depending on the position in the Listview a different template is used.
I have two issues, who are related, since my methods don’t return the right View ID:
If I use adapter.notifyDataSetChanged(); the text in textviews is not adapted, but also the rows outside of the visible screen are not set/adapted. I use the following method to get the View ID:
TextView getText() {
if (title == null) {
switch (templateId) {
case ListViewAdapter.TPL_TITLE:
title = (TextView) row.findViewById(R.id.titletitle);
break;
case ListViewAdapter.TPL_PAYMENT:
title = (TextView) row.findViewById(R.id.titlepayment);
break;
}
}
return (title);
}
In my activity I have different ListItems (a separate class):
items.add(new ListItem(id, title, description, amount, date, template));
I fill the list:
adapter = new ListViewAdapter(this, items);
mainMenu = (ListView) findViewById(R.id.listview);
mainMenu.setAdapter(adapter);
I have this getView method:
public View getView(int position, View convertView, ViewGroup parent) {
templateId=getTemplateID(items.get(position).getType());
View row = convertView;
ListViewWrapper wrapper = null;
if (row == null) {
LayoutInflater inflater = activity.getLayoutInflater();
row = inflater.inflate(R.layout.listviewtemplates, null);
wrapper = new ListViewWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ListViewWrapper) row.getTag();
}
wrapper.setTemplateId(templateId);
wrapper.populateFrom(items.get(position));
return (row);
}
In the wrapper:
public void populateFrom(ListItem r) {
getText().setText(r.getTitle());
if (templateId == ListViewAdapter.TPL_TITLE) {
}
if (templateId == ListViewAdapter.TPL_PAYMENT) {
getSubtext().setText(r.getDescription());
getAmount().setText(r.getAmount());
getDate().setText(r.getDate());
} }
setTemplate();
}
Hopefully this is readable and does anyone understand/recognize my issue. I guess the real question is, how do I get the right ViewID returned per row?
Just realized that if (title == null) {…} does not work, removing that solved the issue