Android 适配器中使用的ImageView中的默认图像

Android 适配器中使用的ImageView中的默认图像,android,android-layout,listview,Android,Android Layout,Listview,我只需要一种在listview中使用的所有imageview(见下文)上显示默认图片的方法 适配器使用此布局,因此在此布局上不调用setContentView函数。我认为这是造成问题的原因(默认图像不会出现)。我怎样才能修好它 图像defaultpic.png位于drawable文件夹中,为100x100px 我的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://sc

我只需要一种在listview中使用的所有imageview(见下文)上显示默认图片的方法

适配器使用此布局,因此在此布局上不调用setContentView函数。我认为这是造成问题的原因(默认图像不会出现)。我怎样才能修好它

图像defaultpic.png位于drawable文件夹中,为100x100px

我的代码:

<?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>

为listviewitem生成的布局具有图像

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

因此,您看不到默认图片

您为listviewitem生成的布局具有该图像

<RelativeLayout...>
    <ImageView android:id="@+id/profile" android:src="@drawable/defaultpic" ... />
</RelativeLayout>
因此,您不会看到默认图片,如k3b所述: 为listviewitem生成的布局具有图像 因此,您不会看到默认图片

纠正这种情况的方法是添加if语句

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

这将导致它仅在不为空时加载配置文件封面,但如果为空,则将使用默认配置文件映像。

如k3b所述: 为listviewitem生成的布局具有图像 因此,您不会看到默认图片

纠正这种情况的方法是添加if语句

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


这将导致它仅在不为空时加载配置文件封面,但如果为空,则将使用默认配置文件映像。

我认为这是导致问题的原因。我怎样才能修好它?子类化一个可用适配器。有完整的示例粘贴getView代码并附加listview@SaurabhKhare粘贴到哪里?他的意思是编辑您的问题并添加适配器getView的当前实现。@SaurabhKhare doneI认为这是问题的原因。我怎样才能修好它?子类化一个可用适配器。有完整的示例粘贴getView代码并附加listview@SaurabhKhare粘贴到哪里?他的意思是编辑您的问题并添加适配器getView的当前实现。@SaurabhKhare完成
@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);
    ...