Kalina
Kalina

Reputation: 5594

How do I remove header view? Or fix this exception: "Cannot add header view to list - setAdapter has already been called"

I am making a search app where you enter a term, and a list (ListView) of results comes up, with a header that labels the list. You can do another search and get a different set of results. Or you can select one of the results and it takes you to another Activity with a ListView of details for that selection. This all works fine.

But when you go back from the details Activity to the search Activity and type in another search term, I getting the following error.

Cannot add header view to list -- setAdapter has already been called.

Both of my Activity classes have a line like

adapter.setAdapter(results);

and to my understanding, that is not allowed. My approach to fixing this would be to remove/clear the offending data (would that be the view? list? adapter?) in the onDestroy() of the details activity. But I don't know how to go about doing that, or if that's even the right solution.

Upvotes: 2

Views: 1971

Answers (1)

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28717

As the exception clearly states, you had already set the adapter for the ListView, and now trying to add a header view to the ListView.

As I understand, you need to add the header view only once, but change only the text/data displayed by the header view for each search result (by changing the data in the adapter). You can reframe your code accordingly.

Upvotes: 2

Related Questions