Reputation: 2979
I have one issues in my application, I am setting header view to listview
dynamically but am getting below error, I have two activities , A and B according my condition I am setting header view to listview
when I am setting headerview in A activity it works fine but when A activity false my condition and go to B activity there my condition is true then come to A activity i need to add header view there I am getting Error................ I have tried to added onStart(), onResume() methods, but still am getting same error..... how to fix it
Java code
on Strat()
{
if (mDrawerList.getHeaderViewsCount()<1) {
TextView headerText = new TextView(mContext);
headerText.setGravity(Gravity.CENTER);
headerText.setTextColor(getResources().getColor(R.color.white_color));
headerText.setPadding(20, 12, 20, 12);
headerText.setTextSize(18);
headerText.setText(mSessionManager.getUserName());
mDrawerList.addHeaderView(headerText);
}
adapter = new NavDrawerListAdapter(mContext, navDrawerItems);
mDrawerList.setDividerHeight(2);
mDrawerList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Error message
2-20 15:15:34.799: E/AndroidRuntime(13111): FATAL EXCEPTION: main
12-20 15:15:34.799: E/AndroidRuntime(13111): java.lang.RuntimeException: Unable to resume activity {com.examle.EventListActivity}: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.os.Looper.loop(Looper.java:137)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-20 15:15:34.799: E/AndroidRuntime(13111): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 15:15:34.799: E/AndroidRuntime(13111): at java.lang.reflect.Method.invoke(Method.java:525)
12-20 15:15:34.799: E/AndroidRuntime(13111): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-20 15:15:34.799: E/AndroidRuntime(13111): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 15:15:34.799: E/AndroidRuntime(13111): at dalvik.system.NativeStart.main(Native Method)
12-20 15:15:34.799: E/AndroidRuntime(13111): Caused by: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.widget.ListView.addHeaderView(ListView.java:258)
12-20 15:15:34.799: E/AndroidRuntime(13111): at android.widget.ListView.addHeaderView(ListView.java:287)
Upvotes: 2
Views: 5732
Reputation: 95
need to check whether the list has already set the adapter or not.
if (mDrawerList.getAdapter==null) {
// add the header view here.this will work
}
Upvotes: 0
Reputation: 32231
How about:
ListAdapter adapter = listView.getAdapter();
listView.setAdapter(null);
listView.addHeaderView(headerView);
listView.setAdapter(adapter);
The only problem here, is that it restart your scrollbar position. But than there is no other way to add headerView after adapter been set.
Upvotes: 4
Reputation: 575
I found this simple workaround here http://code.neenbedankt.com/note-to-self-listfragment-and-header-views/:
@Override
public void onDestroyView() {
super.onDestroyView();
setListAdapter(null);
}
Simply add this to the ListFragment class.
It works for me on 2.3.3 and 4.2.2
Upvotes: 8
Reputation: 15774
Please take a look at the documentation of addHeaderView
.
Give special attention to the note:
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with 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.
Upvotes: 8