0

I just need a way to show default picture on all imageview ( see below ) used in a listview.

This layout is used by an adapter, so no setContentView function is called on this layout. I think this is causing the problem ( the default images doesn't appear ). How can I fix it ?

The image defaultpic.png is in the drawable folder and is 100x100px.

My code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    tools:context=".MainActivity"
    android:background="#222222"
    android:paddingTop="2dp"
    android:paddingBottom="2dp">

    <TextView
        android:id="@+id/profile_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:textColor="#fcdb8c"
        android:background="#222222"
        android:textStyle="normal"
        android:textSize="15sp"
        android:layout_alignParentTop="true"
        android:lines="1"
        android:scrollHorizontally="true"
        android:ellipsize="end"
        android:paddingRight="40dp"
        android:paddingLeft="2dp" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/profile"
        android:scaleType = "fitXY"
        android:src="@drawable/defaultpic"
        android:background="#151515"
        android:padding="1dp" />

</RelativeLayout>


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

    RelativeLayout profileLay = (RelativeLayout)profileInf.inflate
            (R.layout.profile_layout, parent, false);

    TextView profileView = (TextView)profileLay.findViewById(R.id.profile_name);
    ImageView coverView = (ImageView)profileLay.findViewById(R.id.profile);

    profileView.setText(currProfile.getName());
    Drawable img = Drawable.createFromPath(currProfile.getCover());
    coverView.setImageDrawable(img);
    return profileLay;
}
Wasim K. Memon
  • 5,516
  • 4
  • 35
  • 50
xRobot
  • 22,700
  • 56
  • 163
  • 281

2 Answers2

1

your generated layout for the listviewitem has the image

<RelativeLayout...>
    <ImageView android:id="@+id/profile" android:src="@drawable/defaultpic" ... />
</RelativeLayout>

and when the listviewitem is created you immediately overwrite the picture in the imageview

@Override
public View getView(View convertView, ViewGroup parent) {
    ...
    ImageView coverView = (ImageView)profileLay.findViewById(R.id.profile);
    Drawable img = Drawable.createFromPath(currProfile.getCover());
    coverView.setImageDrawable(img);
    ...

therefor you do not see the default picture

k3b
  • 13,724
  • 6
  • 47
  • 81
1

As k3b said: your generated layout for the listviewitem has the image therefore you do not see the default picture.

the way to correct this is to add an if statement.

ImageView coverView = (ImageView)profileLay.findViewById(R.id.profile); if (Drawable.createFromPath(currProfile.getCover()) == null) { Drawable img = Drawable.createFromPath(currProfile.getCover()); coverView.setImageDrawable(img); }

This will cause it to only load the profile cover if it isn't null, but if it is then the default profile image will be used.

Robert
  • 112
  • 4