How to add footer view dynamically
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 开发者_如何学Gowhen 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?
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));
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);
精彩评论