I have a listView. The content of listView is stored in the list maintained in the adapter.I want to add and remove a header of this listView at click of button. When I add header,I get exception that adapter is already set.Is there any other way?Please help.
You have to call .addHeader()
and .addFooter()
before .setAdapter()
on a ListView. My suggestion is to modify your adapter to display a header row as a first row
, when needed, and not use .addHeader(). You may have to add a new row-type for this in your adapter. Also, add methods for your adapter that enables you to hide/show this header row, but don't forget to call .notifyDatasetChanged()
after that.
You have to set the adapter after adding the Header, like this
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView listView = getListView();
listView.addHeaderView(header);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, names));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With