Khronos
Khronos

Reputation: 381

Dynamically hiding text in ListView - Not always working

I'm trying to set a TextView object to disappear in a ListView row if the TextView object has no text inside it.

@Override
public View getView(int position, final View convertView, ViewGroup parent) {

    View row = convertView;

    if (row == null) {
        final LayoutInflater inflater = getLayoutInflater();
        row = inflater.inflate(R.layout.members, parent, false);
    }

    final TextView header = (TextView) row.findViewById(R.id.listHeader);
    header.setText(parsedList.get(position).header);

    final TextView footer = (TextView) row.findViewById(R.id.listDescription); 

    if(parsedList.get(position).footer.length() == 0) {
        footer.setVisibility(View.GONE);
    } else {
        footer.setText(parsedList.get(position).footer);
    }

    return row;
}

Manifest:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
    <TextView
    android:id="@+id/listHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_marginTop="6dp" android:layout_marginBottom="6dp"/>
    <TextView
    android:id="@+id/listDescription"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" android:layout_marginBottom="6dp" android:layout_marginLeft="24dp" android:layout_marginRight="12dp"/>

</LinearLayout>

When the Activity loads, everything displays as it should (Most of the time). However when I rotate the screen, some of the footer TextView objects disappear from the screen. This is even though when I run the debugger, footer.setVisibility(View.GONE); is never called.

Also in my manifest I have android:configChanges="orientation|keyboardHidden" set for the Activity.

EDIT: This issue also rarely occurs when the Activity is created. However it always occurs on screen rotation.

Upvotes: 0

Views: 459

Answers (3)

Shaiful
Shaiful

Reputation: 5673

Replace With This.

if(parsedList.get(position).footer.length() == 0) {
        footer.setVisibility(View.GONE);
    } else {
        footer.setText(parsedList.get(position).footer);
        footer.setVisibility(View.VISIBLE);
    }

Upvotes: 1

Jaypeg
Jaypeg

Reputation: 13

Have you tried using getText().length() in the if statement?

if(parsedList.get(position).footer.length() == 0)

Upvotes: 0

Vinit ...
Vinit ...

Reputation: 1459

Override this method in Activity:

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

Upvotes: 0

Related Questions