views:

21

answers:

1

My XML layout in res/layout/edit.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView 
  android:id="@+id/GridView01"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:numColumns="2">
<TextView
  android:text="Person's Name"
  android:id="@+id/PersonName"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
<TextView
  android:text="Person's Points"
  android:id="@+id/PersonPoints"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></TextView>
</GridView>

When I try to switch from "edit.xml" to "Layout", I see this error:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not
supported in AdapterView

I checked the documentation for AdapterView, and this is expected behavior. Why, then, does ADT expect it to work?

+1  A: 

It's because you're trying to add two TextViews to it in the xml. Don't do that. The views for any AdapterView have to come from the adapter.

You'll have to put your TextViews in another Layout that contains your GridView as well. Layouts you can use are: FrameLayout, LinearLayout, RelativeLayout, TableLayout, or implement your own. I think what you are trying to do is have two buttons on top of a GridView that you will be providing an Adapter that will fill it.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" 
        />
    </LinearLayout>
    <GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>

The two TextViews will be the same size because of the layout_weight attribute. The following GridView will take up the rest of the space.

If all you were trying to achieve was having the two TextViews next to each other but being the same size then just use the second LinearLayout with the horizontal orientation.

Qberticus
So <?xml blah?> <ViewGroup blah> <GridView blah /> <TextView blah /> <TextView blah /> </ViewGroup>?
Matt
See if this extended answer helps clear things up. :)
Qberticus
Actually, I think I want TableLayout, not GridView. This looks like a bit of a fiasco as it is. Thanks.
Matt