2

I have a Listview that I am populating with a custom layout

<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView android:src="@drawable/message_bg" 
    android:layout_width="fill_parent" 
    android:id="@+id/cellbg" 
    android:layout_alignTop="@+id/userPhoto" 
    android:layout_alignBottom="@+id/username" 
    android:layout_below="@+id/userPhoto" 
    android:layout_height="fill_parent" 
    android:scaleType="centerCrop"></ImageView>

    <ImageView android:src="@drawable/default_photo" 
    android:id="@+id/userPhoto" 
    android:layout_alignParentLeft="true" 
    android:layout_width="40.0sp" 
    android:layout_height="40.0sp" 
    android:scaleType="center"></ImageView>

    <TextView android:text="TextView" 
    android:id="@+id/messagePreview" 
    android:textSize="20.0sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/userPhoto" 
    android:layout_alignTop="@+id/userPhoto" 
    android:lines="1"></TextView>

    <TextView android:text="TextView" 
    android:id="@+id/username"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/messagePreview" 
    android:layout_alignLeft="@+id/messagePreview" 
    android:lines="1"></TextView>
</RelativeLayout>

That image view +@id/cellbg is spontaneously disappearing in some of my rows. I am also using a custom list adapter:

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

    if (null == convertView)
    {
        row = mInflater.inflate(R.layout.messagecell, null);
    }
    else
    {
        row = convertView;
    }

    Map<String,String> message = messageList.get(position);

    TextView tv = (TextView) row.findViewById(R.id.username);
    //tv.setTextColor(Liveblogging_Main.forColor);
    tv.setText(message.get("CreatedBy"));

    TextView tv2 = (TextView) row.findViewById(R.id.messagePreview);
    //tv2.setTextColor(Liveblogging_Main.forColor);
    tv2.setText(message.get("Description"));

    ImageView iv = (ImageView) row.findViewById(R.id.userPhoto );
    Drawable userIcon = Liveblogging_Main.GetImageForUser(message.get("CreatedBy"));
    if ( userIcon == null )
    {
        GetUserImage(message.get("CreatedBy"));
        userIcon = getResources().getDrawable(R.drawable.default_photo);
    }
    iv.setImageDrawable(userIcon);
    LayoutParams params = iv.getLayoutParams();
    params.width = 40;
    params.height = 40;
    iv.setLayoutParams(params);
    iv.setScaleType(ScaleType.CENTER_CROP);

    ImageView bg = (ImageView) row.findViewById(R.id.cellbg );
    bg.setImageDrawable(getResources().getDrawable(R.drawable.message_bg));

    return row;
}

You can see in there I am forcibly setting the imagedrawable on the imageview, yet it is still disappearing during scrolling sometimes. Any Ideas?

EDIT:

Here is also my listview code

<LinearLayout   android:id="@+id/linearLayout1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView   android:id="@android:id/list" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:cacheColorHint="#00000000"
    >
    </ListView>

</LinearLayout>
Dan F
  • 17,654
  • 5
  • 72
  • 110
  • I know this is a simple answer, but... is there any reason you're setting the BG image in the code? can you not try setting it in the xml? – Martyn May 23 '11 at 14:56
  • It is set in the xml as well, I tried adding it into the code as well just to make sure its not getting overwritten somewhere in the inflation: `ImageView android:src="@drawable/message_bg"` – Dan F May 23 '11 at 14:57
  • Ah sorry - didn't spot that. I've had loads of issues with images in list views before. Will try and see if I can find an example where I've seen this before – Martyn May 23 '11 at 15:10

6 Answers6

1

Try to set XML attribute

android:scrollingCache="false"

in your ListView.

kamil zych
  • 1,682
  • 13
  • 21
0

Try setting android:cacheColorHint="#00000000" on your listview.

Sashi Kolli
  • 751
  • 5
  • 6
0

Try this

bg.setImageResource(R.drawable.message_bg);
Fedor
  • 43,261
  • 10
  • 79
  • 89
0

I think you can draw the background image to the RelativeLayout itself. Try something like this:

<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/message_bg" xmlns:android="http://schemas.android.com/apk/res/android">
...

And remove the ImageView with +@id/cellbg.

Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26
0

Try with this code:

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

    if (null == convertView)
        row = mInflater.inflate(R.layout.messagecell, null);
    else
        row = convertView;

    Map<String,String> message = messageList.get(position);

    TextView tv = (TextView) row.findViewById(R.id.username);
    tv.setText(message.get("CreatedBy"));

    TextView tv2 = (TextView) row.findViewById(R.id.messagePreview);
    tv2.setText(message.get("Description"));

    ImageView iv = (ImageView) row.findViewById(R.id.userPhoto );
    Drawable userIcon = Liveblogging_Main.GetImageForUser(message.get("CreatedBy"));
    if ( userIcon != null )
        iv.setImageDrawable(userIcon);
    else
        iv.setImageResource(R.drawable.default_photo);

    return row;
}

And your View:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/message_bg">
<ImageView
    android:src="@drawable/default_photo"
    android:id="@+id/userPhoto"
    android:layout_alignParentLeft="true"
    android:layout_width="40.0sp"
    android:layout_height="40.0sp"
    android:scaleType="center"></ImageView>

<TextView
    android:text="TextView"
    android:id="@+id/messagePreview"
    android:textSize="20.0sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/userPhoto"
    android:layout_alignTop="@+id/userPhoto"
    android:lines="1"></TextView>

<TextView
    android:text="TextView"
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/messagePreview"
    android:layout_alignLeft="@+id/messagePreview"
    android:lines="1"></TextView>
</RelativeLayout>
vsm
  • 3,373
  • 2
  • 25
  • 36
0

Please, try to use viewHolder like in below website example :

http://www.anddev.org/view-layout-resource-problems-f27/listview-with-image-and-text-problem-t13694.html

This will help you to solve the problem.

Thanks,

Bipin Vayalu
  • 3,025
  • 2
  • 25
  • 39