3

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" />
Community
  • 1
  • 1
user1725145
  • 3,993
  • 2
  • 37
  • 58

1 Answers1

3

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.

Community
  • 1
  • 1
matthewrdev
  • 11,930
  • 5
  • 52
  • 64
  • 3
    Thank you. It will take me a little while to absorb this info, so I will be back later. I do find the Xamarin docs hard, they always seem to assume knowledge I don't have. – user1725145 Jan 05 '16 at 23:02
  • 1
    Stick with it S List, it *does* take a little while to learn it all. The difficultly curve is tough because it covers multiple frameworks, languages and operating systems; we've all been there! Just keep chipping at it :) – matthewrdev Jan 06 '16 at 00:01
  • I enclosed the TextView of the header in a RelativeLayout, and created an id for it. Then I passed this id to the Inflater. Also, I separated the text and the image into separate headers. I already have an adapter. – user1725145 Jan 06 '16 at 15:51
  • After doing these changes, I got an "invalidOperation" exception, which was solved by Clint's answer from this SO question: http://stackoverflow.com/questions/4306324/unable-to-use-layoutinflater-in-custom-adapter/6419586#6419586 – user1725145 Jan 06 '16 at 15:52
  • Inflating and adding the headers now works. Thank you very much for this great answer, and your kind words of encouragement. – user1725145 Jan 06 '16 at 15:53