Reputation: 133
Anyone know how to properly add a HeaderView to a ListView in android? This is what I have:
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.adapter_post_note, commentsListView, false);
after that I setup an ImageView and a couple TextView's that are already part of the adapter_post_note layout by setting the image resource and/or text. then here is where I add the HeaderView
commentsListView.addHeaderView(inflatedView);
commentsAdapter = new CommentsAdapter(this, post.getComments(), post);
commentsListView.setAdapter(commentsAdapter);
The adapter is a custom adapter that extends BaseAdapter and I pass in the context, the List that populates the ListView, and the "post" for some reason, but it doesnt matter.
here is the error i am getting
07-16 12:39:58.858: E/AndroidRuntime(1562): FATAL EXCEPTION: main
07-16 12:39:58.858: E/AndroidRuntime(1562): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.LinearLayout.measureVertical(LinearLayout.java:360)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.View.measure(View.java:8372)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.os.Looper.loop(Looper.java:130)
07-16 12:39:58.858: E/AndroidRuntime(1562): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-16 12:39:58.858: E/AndroidRuntime(1562): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 12:39:58.858: E/AndroidRuntime(1562): at java.lang.reflect.Method.invoke(Method.java:507)
07-16 12:39:58.858: E/AndroidRuntime(1562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-16 12:39:58.858: E/AndroidRuntime(1562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-16 12:39:58.858: E/AndroidRuntime(1562): at dalvik.system.NativeStart.main(Native Method)
I have tried setting the layout params of inflatedView to ListView.LayoutParams, FrameLayout.LayoutParams, AbsListView.LayoutParams, nothing works. It seems to me as if it is adding the inflatedView as the header just fine, but then later on when it attempts to invalidate the screen, it tries to go through and recycle ListView children states and it fails, that just a guess, but any thoughts?
Also, one more thing I have tried is
View.inflate(this, R.layout.adapter_post_note, null);
but it still doesnt work
Upvotes: 0
Views: 1180
Reputation: 50548
Set the parent view to null and you are good to go. No need to reference the parent of the view. Just inflate it and pass it to the method.
Upvotes: 1