RileyE
RileyE

Reputation: 11084

XML Issue: Loading layout for ListView

I am probably just misusing the XML, but I'm trying to create a layout for a list view row, but I seem to be failing miserably.

I have this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp" >

    <TextView android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/detailsTextView"
        android:textSize="16sp"/>

    <TextView android:id="@id/detailsTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"/>


</RelativeLayout>

In hopes that I will be able to add a layout that is 100dp in height to my ListView and have two TextViews inside of it. However, when I try to set it up like so:

public View getView(int position, View currentView, ViewGroup parent)
{
    View rowContents = LayoutInflater.from(mContext).inflate(R.layout.list_view_cell, null);
    TextView titleView = (TextView)rowContents.findViewById(R.id.titleTextView);
    titleView.setText(titles[position]);
    TextView detailsView = (TextView)rowContents.findViewById(R.id.detailsTextView);
    detailsView.setText(details[position]);

    return rowContents;
}

It's giving me a row with only my detailsView TextViewwith a wrap_content height, it seems. The titleView is nowhere to be seen and I would assume the rowContents isn't being set either, otherwise the height would be 100dp, right?

I know I should be reusing the old view and setting its contents, but I just made this up quickly as a test.

What is going wrong here?

Upvotes: 0

Views: 67

Answers (1)

Sam
Sam

Reputation: 86958

Try setting the lower TextView with layout_below instead:

<TextView android:id="@+id/titleTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"/>

<TextView android:id="@+id/detailsTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/titleTextView"
    android:textSize="12sp"/>

(Otherwise you could "lock" titleTextView into the upper lefthand corner, which might force detailsTextView to move down.)

But you should watch this Google Talk by one of Android's lead programmers, it gives great detail on how to make an efficient adapter.

Upvotes: 2

Related Questions