I have one edittext field and one \"search\" button. When I click on search, I have to display a list view with data corresponding to the values entered in the edittext. I h
Cannot add header view to list -- setAdapter has already been called. which you can see, the myList.addHeaderView(header) must be execute before myList.setAdapter(adapter);
I have used the following code in my sample application to set a ListView header:
ListView lv = getListView();
View headerView = getLayoutInflater().inflate(R.layout.layout_header, null, false);
lv.addHeaderView(headerView);
final TumblrDB db = new TumblrDB(this);
c = db.query();
startManagingCursor(c);
adapter = new CustomCursorAdapter(this, R.layout.layout_del, c, new String[]{TumblrDB.DATE, TumblrDB.DESC}, new int[]{R.id.txt_a, R.id.txt_b});
lv.setAdapter(adapter);
The exception is thrown by android api.For API Level below KITKAT the addHeader() or the addFooter() method must be called before setAdapter() method.
It is mentioned in the api documentation:
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with Build.VERSION_CODES.KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Try this..
dataAdapter = new MyCustomAdapter(this, R.layout.results_list_item, searchedResults);
myList.addHeaderView(header);
myList.setAdapter(dataAdapter);
dataAdapter.notifyDataSetChanged();
you can add FrameLayout as header view before setting adapter and dynamically add/remove view in FrameLaypout
After I set
final ViewGroup header = (ViewGroup) inflater.inflate(R.layout.item, listView, false);
listView.addHeaderView(header, null, true);
before
listView.setAdapter(adapter);
a problem still appeared. Then I made Build > Clean Project.