Reputation: 9548
I have different buttons in my header view for sorting the entries within the list. The problem is that when I am re-adding the header view to the listview, the header view will appear multiple times (it depends how many times I am sorting the list), which isn't the right way. The header view should appear once.
What have I tried:
// Create a new instance of a sorting view
SortingView header = new SortingView(getActivity(), new int[] {R.id.btnAsc, R.id.btnDesc, R.id.btnAll});
// If an item is pressed, then collapse the last expanded group view
header.setOnItemClickListener(new View.OnClickListener(){
@Override(View v) {
expListView.collapsGroupView(mLastExpandedView);
}
});
// The exp list-view is having a header view
// REMOVE THE HEADER VIEW
if (expListView.getHeaderViewsCount() != 0) {
expListView.removeHeaderView(header);
}
// Add the new header view
expListView.addHeader(header);
expListView.setAdapter(mEntries);
It's not working... I don't know what should I do.
Note: I don't want to hide/show the header view, because I am passing multiple listeners (event listeners), objects, etc. and I want to make a new instance of this class.
Upvotes: 2
Views: 1390
Reputation: 8685
You aren't removing the previously-added header view; you are attempting to remove the newly-created instance of SortingView. Look:
SortingView header = new SortingView(getActivity(), new int[] {R.id.btnAsc, R.id.btnDesc, R.id.btnAll});
...
expListView.removeHeaderView(header);
If this instance of SortingView has not yet been added as a header, how do you expect to remove it?
You need to store references to your previously-added headers so that you can remove them.
Upvotes: 1