I'm adding a textview as a header to a listview, in a Xamarin Android app. Following the instructions in ericosg's answer to this SO question, I put the textview in a separate axml file, and then tried to add it as a header to my list view.
Running the app gets the following error at the line where the activity tries to inflate the textview:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Android.Content.Res.Resources+NotFoundException: Exception of type 'Android.Content.Res.Resources+NotFoundException' was thrown.
.
.
.
[MonoDroid] --- End of managed exception stack trace ---
[MonoDroid] android.content.res.Resources$NotFoundException: Resource ID #0x7f050006 type #0x12 is not valid
[MonoDroid] at android.content.res.Resources.loadXmlResourceParser(Resources.java:2250)
etc.
Here is my code:
In the Activity .cs file:
myListView = FindViewById<ListView>(MyApp.Resource.Id.MyList);
Android.Views.View myHeader =
this.LayoutInflater.Inflate(MyApp.Resource.Id.QuestionText, myListView);
myListView.AddHeaderView(myHeader);
ListView definition:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/MyList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</RelativeLayout>
Header definition:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/QuestionText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableTop="@+id/MyImage" />
A few things:
You are attempting to inflate an ID resource instead of a layout:
Android.Views.View myHeader = this.LayoutInflater.Inflate(MyApp.Resource.Id.QuestionText, myListView);
The Inflate call for LayoutInflater only accepts Resource.Layout elements. Change it to:
Android.Views.View myHeader = this.LayoutInflater.Inflate(Resource.Layout.Header, myListView);
Secondly, in your Header.xml layout file, TextView android:drawableTop does not accept id references so it will throw an InflateException when the LayoutInflator attempts to build the layout.
From the docs:
android:drawableTop
The drawable to be drawn above the text.
May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
Change this to either a reference to a valid color or drawable (@drawable/MyImage) or an inline color declartion (#fff).
Lastly, you can't add child views or header views to the ListView without using an adapter:
Consider re-reading the Xamarin docs for ListViews and Adapters to better understand the subject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With