Search code examples
androidlistviewconvertview

ConvertView items partially updated


I have custom ListView in my activity and it uses ConvertView pattern and ViewHolder. Everything works fine, but sometimes text in items are cuted off. This is clearly seen on screenshots:

screenshot

It looks like it reuse old view and don't update text length. Here is part of adapter code:

if (convertView == null) {
     viewHolder = new ViewHolderItemBool();

     LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
     if (changeable) {
         convertView = inflater.inflate(R.layout.sensor_bool_e, null, true);
     } else {
         convertView = inflater.inflate(R.layout.sensor_bool, null, true);
     }

     viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
     viewHolder.txtDesc = (TextView) convertView.findViewById(R.id.txtDescript);
     viewHolder.imgState = (ImageView) convertView.findViewById(R.id.img);
     convertView.setTag(viewHolder);
} else {
     viewHolder = (ViewHolderItemBool) convertView.getTag();
}
viewHolder.txtName.setText(name);
if (isImportant()) viewHolder.txtName.setTypeface(null, Typeface.BOLD);
//some code to change description field and picture
return convertView;

And here is item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sensor_bool"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:background="@drawable/sensor_background"
    android:longClickable="true">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/circle_green"
        android:padding="10dp"/>

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

        <TextView
            android:id="@+id/txtName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/sensor_text_size"
            android:singleLine="true"
            android:textColor="@color/sensor_name_color"/>

        <TextView
            android:id="@+id/txtDescript"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingStart="2dp"
            android:textSize="@dimen/sensor_desc_size"
            android:textColor="@android:color/secondary_text_light"/>
    </LinearLayout>
</LinearLayout>

If I don't use ConverView and inflate layout each time it looks fine. Have any ideas how to fix that?


Solution

  • I made it! It was just few stupid errors.

    First: I changed

    android:layout_width="fill_parent"
    

    in txtName to

    android:layout_width="wrap_content"
    

    This solves problem with text width. I do not understand why it did not work before?

    And second:

    if (isImportant()) viewHolder.txtName.setTypeface(null, Typeface.BOLD);
    

    to

    if (isImportant()) {
        viewHolder.txtName.setTypeface(null, Typeface.BOLD);
    } else {
        viewHolder.txtName.setTypeface(null, Typeface.NORMAL);
    }
    

    This solves the problem with bold text. (The problem is don't described in question but seen on screens.)

    Thanx to all for help!