How to add multiple header views in a ListView
android, android-listview
Solution
I don't think what you want to do is possible the way you are trying to do it. When you use `addHeaderView` it wraps your `ListAdapter` in `HeaderViewListAdapter`. I looked at the docs for it here and that seems to imply that you could have multiple headers, but they would all be at the top (duh, header).
It sounds like what you actually want is seperators...
You could use CommonWare's MergeAdapter. It will let you insert adapters and views (in whatever order you wish) and present them all as a single adapter to a listview. You just hand it headers and adapters for each section of content and then set it to your list.
Pseudo-code example:
myMergeAdapter = new MergeAdapter();
myMergeAdapter.addView(HeaderView1);
myMergeAdapter.addAdapter(listAdapter1);
myMergeAdapter.addView(HeaderView2);
myMergeAdapter.addAdapter(listAdapter2);
setListAdapter(myMergeAdapter);
Problem
I've a custom adapter for my `ListView` I want to add project names as the headers to my work requests. Adding a single header works just fine but I'm not sure how to add multiple headers using `addHeaderView`. I don't understand where exactly to place `setAdapter` or is it supposed to be placed multiple times? This is my java code for a single header which works: ``` mListView = (ListView)findViewById(R.id.dashboardList); View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); tv = (TextView) header1.findViewById(R.id.listHeader); adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); tv.setText("Project 1"); mListView.addHeaderView(header1, null, false); for (int i=0; i < 7; i++) { dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); } mListView.setAdapter(adapter); ``` Now, I for two headers I tried this: ``` mListView = (ListView)findViewById(R.id.dashboardList); View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); tv = (TextView) header1.findViewById(R.id.listHeader); adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); tv.setText("RxOffice"); mListView.addHeaderView(header1, null, false); for (int i=0; i < 4; i++) { dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); } tv.setText(Project 2"); mListView.addHeaderView(header1, null, false); for (int i=4; i < workRequests.length; i++) { dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); } mListView.setAdapter(adapter); ``` But this doesn't work! It gives me only the Project 2 header and all 7 entries below it. Could anyone please tell me what's wrong? I'm guessing it has something to do with `setAdapter`. Thanks!