2016-11-09 21 views
0

我只需要一種方法來顯示默認圖片在所有用於列表視圖的圖像(見下文)。在適配器中使用的ImageView中的默認圖像

此佈局由適配器使用,因此在此佈局上不調用setContentView函數。我認爲這是造成問題的原因(默認圖像沒有出現)。我該如何解決它?

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


@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; 
} 
+0

*我認爲這是造成這個問題的原因。我該如何解決它?*繼承其中一個可用的適配器。 [這個問題](http://stackoverflow.com/questions/16338281/custom-adapter-getview-method-is-not-called/16338380#16338380)有一個完整的例子 – Blackbelt

+0

粘貼getView代碼並附上listview的圖像 –

+0

@SaurabhKhare粘貼在哪裏? – xRobot

回答

1

比如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); }

這將導致它,如果它不爲空只加載配置文件覆蓋,但如果是則將使用默認的配置文件的圖像。

1

爲ListViewItem的您生成的佈局具有圖像

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

,並在創建ListViewItem的時候你馬上覆蓋畫面中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); 
    ... 

爲此你看不到的默認圖片

相關問題