I need to be able to add and remove headers and footers from my ListView dynamically.
So I initialize my activity with my headers and footers, then at some point I want to hide them, and later I need to add the previous headers and footers, and keep the same Adapter.
So I found this solution, but it's ugly and I really hope that there is an other way.
Basically, I have to set a null adapter to be able to add the header view, and then set an empty adapter to add the footer view. To finish I set my real adapter.
Edit: I must add that using the visibility attribute (GONE & VISIBLE) is not a solution here, because the headers & footers views must not be in the adapter during my intermediate procedure.
public class TestAdapterHeader extends ListActivity implements OnClickListener {
private static String[] items = { "test 1", "test 2", "test 3", "test 4",
"test 5", "test 6", "test 7", "test 8", "test 9", "test 10",
"test 11", "test 12", "test 13", "test 14", "test 15", "test 16",
"test 17", "test 18", "test 19", "test 20" };
private ArrayAdapter mAdapter;
private LinearLayout mParentView;
private TextView mHeaderView, mFooterView;
private boolean mViewsHidden = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initViews();
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
setListAdapter(mAdapter);
}
private void initViews() {
// The main layout
mParentView = new LinearLayout(this);
mParentView.setOrientation(LinearLayout.VERTICAL);
mParentView.setBackgroundColor(Color.BLACK);
// The button to hide the views
Button hideViewsButt