I’ve to make an application that shows information about a F1 race. In order to display this information I’ve used a ListView with several SimpleAdapters that show different data sets (one position, name, time; another each sector time…).
adaptadorPortrait1 = new SimpleAdapter(
getApplicationContext(),
lista_adaptador,
R.layout.portrait1,
new String[] {"pos", "short_name", "time_total", "time_pred"},
new int[] {R.id.pos, R.id.name, R.id.time,R.id.prev});
adaptadorPortrait2 = new SimpleAdapter(
getApplicationContext(),
lista_adaptador,
R.layout.portrait2,
new String[] {"pos", "short_name", "sect1","sect1","sect3"},
new int[] {R.id.pos, R.id.name, R.id.psect, R.id.ssect, R.id.tsect});
[...]
When some events such as slice a finger over the screen or to rotate the mobile, occur, I change the list adapter.
Now I want to add a header to the list to indicate what is each column and I need that this header changes at the same time that adapter does. I have tried to use the ´addHeaderView´ method but the app fails and it throws an IllegalStateException with the message “Cannot add header view to list — setAdapter has already been called.”
Any ideas?
PS: Please excuse my bad English.
ListViews support multiple types of views within a single Adapter.
If you override the
adapter.getItemTypeCountandadapter.getItemType(int position)methods, you can add headers in-line with the rest of your views. So, the first item within each of your adapters would be your “header” type of view (maybe a simpleTextView) and the rest could be your list entries.You’d just have to make sure that
getItemTypereturns a different number depending on whether the position (which for a header would be 0) is a header or not.