Search code examples
androidandroid-cursoradapterandroid-inflate

Inflating LinearLayout for CursorAdapter


I'm trying to create a view from CursorAdapter.

However, when I inflate a layout, I'd like to inflate LinearLayout instead of the whole .xml layout. For example,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/opera_house_background">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/account_balance"
        android:textColor="#FFFFFF"
        android:id="@+id/balance_text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:textSize="30sp"/>

    <TextView
        android:id="@+id/balance_money"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/balance_text"
        android:textSize="25sp"
        android:text=""
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"/>


    <TextView
        android:id="@+id/transaction_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/balance_money"
        android:text="@string/transaction_detail_text"
        android:textColor="#000000"
        android:padding="5dp"
        android:textSize="15sp"
        android:layout_marginTop="30dp"
        android:background="@drawable/divider"/>


    <LinearLayout
        android:id="@+id/transaction_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/transaction_text"
        android:padding="10dp"
        android:baselineAligned="false">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:layout_weight="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/location"
                android:text="Location"
                android:textSize="18sp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/date"
                android:text="Date"
                android:textSize="18sp"
                android:layout_marginTop="5dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/time"
                android:text="Time"
                android:textSize="18sp"
                android:layout_marginTop="5dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center_horizontal|center_vertical"
            android:layout_weight="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="- 12"
                android:id="@+id/money"
                android:textSize="25sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Here with LinearLayout with ID "transaction_text", I want just that part of the layout to be shown as an adapter. Please help!

Thanks in advance!


Solution

  • Step #1: Move that LinearLayout into a separate layout resource. For the purposes of this answer, I will call it foo.

    Step #2: Where the LinearLayout was in the above code listing, replace it with <include layout="@layout/foo"/>, using the <include> tag to pull in another layout resource.

    Step #3: In your CursorAdapter, use foo as your layout.