I am trying to get a handle on the RelativeLayout that defines how a row in a ListView should be laid out. I am getting null and I believe it is because one of two things.
Possibility one is that it is not inflated yet so it returns null….While I’ve read about this one I am very shaky on the inflation concept. I have been programming in Java for 3 weeks now (from C#).
So I’m not sure how to test, much less handle. For example all I do is set the adapter to the list view….not sure what event or when exactly the inflation happens under the covers.
Possibility two is that I’m not walking the tree properly.
My Parent:
<RelativeLayout
android:minHeight="320dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/rlay_LftDataParent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lstvw_LiftData" />
The row definition (the layout I am trying to set the min height for)
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/rlay_LftDataRw"
android:layout_height="fill_parent"
android:background="@drawable/background">
...
</RelativeLayout>
In the Activity I’ve tried: (lstvw_LiftData is my ListView object)
RelativeLayout RowLayout = (RelativeLayout)lstvw_LiftData.findViewById(R.id.rlay_LftDataRw);
and
RelativeLayout RowLayout = (RelativeLayout)findViewById(R.id.rlay_LftDataRw);
If you are curious to know the bigger picture this is a follow on question from this post Using RelativeLayout to control a ListView row height where I’m trying to get the row layout to fill the screen.
You don’t access a
Viewfrom theListViewrow withfindViewById. Instead use the adapter to get a reference of yourRelativeLayoutin thegetViewmethod and there set the width and height. You didn’t say what adapter do you use so here is a simple example for the basicArrayAdapter:Edit:
You don’t call the
getView()method yourself, the adapter will call thegetViewmethod automatically when it’s time to show a new row. Regarding the question from your last comment, if the customAdapterclass is a inner class in theActivitywhere you’ll use it:If you
Adapterclass is in another file then make the constructor of that adapter to take twointvalues representing the width and height:and in your
Activity(with the same code for the onCreate method):I don’t know if this is what you want.