Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add and remove a header of this listView

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.

like image 258
ShineDown Avatar asked Dec 01 '22 01:12

ShineDown


2 Answers

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.

like image 65
Zsombor Erdődy-Nagy Avatar answered Dec 05 '22 01:12

Zsombor Erdődy-Nagy


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));
like image 31
Lalit Poptani Avatar answered Dec 05 '22 00:12

Lalit Poptani