How to omit sublayout from row in androidlist through SimpleCursorAdapter?

55 Views Asked by At

I have a ListFragment, where each row is formed by several data retrieved from the database. This data is structured in sub-layouts, and one of them consist of a LinerLayout with a fixed text and a dynamic text to be populated from the db, something like Value: X where X is the value coming from the database.

However that value is optional in the db, and in case the value is not present, I would like not to present the fixed text.

The question was how to do that in the SimpleCursorAdapter responsible for the layout inflate of the rows.

Here is the layout of the rows, where the sub-layout composed by the fixed and dynamic texts is the one with the id valueLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="Text"
            android:textColor="#000000"
            android:textSize="13sp"
            android:textStyle="bold" >

        </TextView>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/alert"
                android:text="Alert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/description"
                android:text="Desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:textSize="12sp" />
        </LinearLayout>

          <LinearLayout
            android:id="@+id/valueLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/valueFixedText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Value: "
                android:padding="3dp"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:textSize="12sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/timestamp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="date"
            android:textSize="8sp" />
    </LinearLayout>


</RelativeLayout>

I could not find the answer directly here in SO and had to hunt down some pieces of information. Therefore, I decided to share the answer here as well

1

There are 1 best solutions below

0
On

The solution basically consisted on overwriting the bindView method from the SimpleCursorAdapter, checking for the value in the cursor, and if it was not present, get the sub-layout and set its visibility to View.GONE

public class MyCursorAdapter extends SimpleCursorAdapter {

    public MyCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
        super.bindView(v, context, c);
        // the value is empty, we clear its linear layout
        String value = c.getString(c.getColumnIndex(DATA.VALUE));
        if(null == value || value.isEmpty()){           
            LinearLayout valueLayout = (LinearLayout) v.findViewById(R.id.valueLayout);
            valueLayout.setVisibility(View.GONE);
        }
    }   
}