1

I have created a ListView and added header with addHeaderView, then I called setListAdapter in my ListActivity. Any idea how can I dynamically addFooterView after I called setListAdapter?

ANSWER: I added both header view and footer view (actually buttons) into my list view, but both of them I wrapped into a FrameLayout using wrap_content height, then when I do not need to be the header button to be shown I just setVisibility(View.GONE) and FrameLayout wraps to 0 height and vissualy it is not visible (same effect as if I would call removeHeaderView), and if I need to show it again I setVisibilty(View.VISIBLE) and it is shown (same effect as addHeaderView - which is of course not possible after calling setting list adapter)

Discussed here: Hide footer view in ListView?

Community
  • 1
  • 1
webaloman
  • 221
  • 1
  • 4
  • 14

2 Answers2

4
View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();

listView.addHeaderView(header);
listView.addFooterView(footer);    
setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1, names));
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 1
    09-25 18:43:52.953: ERROR/AndroidRuntime(229): java.lang.IllegalStateException: Cannot add footer view to list -- setAdapter has already been called. – webaloman Sep 25 '11 at 19:26
  • @webaloman - look at my edited answer. try this and let me know what happen. – user370305 Sep 25 '11 at 19:38
  • Just remember that footer and header are also indexed as a list element. So, If you click on the first item in list below the footer, that item index will be 1, but not 0. –  Jan 18 '12 at 13:52
0

You Have to do like this

View header = (View)getLayoutInflater().inflate(R.layout.header,null);

SimpleAdapter myAdapter=new SimpleAdapter(this,myList,R.layout.transactionvalues,
new String[] {"transaction_date_time","user_name","site_name","machine_name"},new int[] {R.id.Date_Time,R.id.User,R.id.Site,R.id.Machine});

if(header == null){
    lst.removeHeaderView(header);
}else
{
    lst.addHeaderView(header,null,false);
}

lst.setAdapter(myAdapter);
Stephen
  • 1,737
  • 2
  • 26
  • 37
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45