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 then I need to add the previous headers and footers and keep the same Adapter .
So, I found this solution, but it is ugly, and I really hope that there is another way.
Basically, I have to install a null adapter in order to be able to add a header view, and then install an empty adapter to add a footer view. To finish, I installed my real adapter.
Edit: I have to add that using the visibility attribute (GONE and VISIBLE) is not the solution here, because the header and footer views should 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() {
android listview
Chayy
source share