Pinki
Pinki

Reputation: 21929

Add the imageview as the header of the listview

Hi I am developing one app using the below link Custom ListView With Headers

Now My question is i want to set the image as header for the listview.I am display the list of items using the second type of addsection i.e;

adapter.addSection("Security", new SimpleAdapter(this, security, R.layout.list_complex,  
            new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption })); 

I am using the image view instead of textview in list_headre.xml.at that time i got the following exception

02-28 05:43:12.273: ERROR/ArrayAdapter(1212): You must supply a resource ID for a TextView
02-28 05:43:12.273: DEBUG/AndroidRuntime(1212): Shutting down VM
02-28 05:43:12.273: WARN/dalvikvm(1212): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-28 05:43:12.283: ERROR/AndroidRuntime(1212): Uncaught handler: thread main exiting due to uncaught exception
02-28 05:43:12.322: ERROR/AndroidRuntime(1212): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at chronicle.books.shopping.SeparatedListAdapter.getView(SeparatedListAdapter.java:97)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.AbsListView.obtainView(AbsListView.java:1274)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ListView.makeAndAddView(ListView.java:1668)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ListView.fillDown(ListView.java:637)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ListView.fillFromTop(ListView.java:694)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ListView.layoutChildren(ListView.java:1521)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ScrollView.onLayout(ScrollView.java:1108)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.View.layout(View.java:6830)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.os.Looper.loop(Looper.java:123)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at java.lang.reflect.Method.invoke(Method.java:521)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at dalvik.system.NativeStart.main(Native Method)
02-28 05:43:12.322: ERROR/AndroidRuntime(1212): Caused by: java.lang.ClassCastException: android.widget.ImageView
02-28 05:43:12.322: ERROR/AndroidRuntime(1212):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)

Now I want to display the image as header instead of TextView.For this please give me some code suggestions.Thanks in advance

Upvotes: 1

Views: 1301

Answers (1)

Jason Hanley
Jason Hanley

Reputation: 3225

ArrayAdapter only supports TextView views. As per the docs: To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

If you just want a single ImageView in the header, I would use my original answer below.

Original answer: Use the method addHeaderView in ListView and just pass in an ImageView.

Upvotes: 2

Related Questions