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
The solution basically consisted on overwriting the
bindView
method from theSimpleCursorAdapter
, checking for the value in the cursor, and if it was not present, get the sub-layout and set its visibility toView.GONE