2016-11-09 7 views
0

リストビューで使用されるすべての画像ビュー(以下を参照)にデフォルト画像を表示する方法が必要です。ImageViewのデフォルト画像がアダプタで使用されています

このレイアウトはアダプタで使用されるため、このレイアウトではsetContentView関数は呼び出されません。私はこれが問題を引き起こしていると思います(デフォルトの画像は表示されません)。どうすれば修正できますか?

画像defaultpic.pngはドロウアブルフォルダにあり、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コードを貼り付けてリストビューの画像を添付します –

+0

@SaurabhKhare貼り付け場所? – xRobot

答えて

1

が表示されていないと述べた。 の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); }

これは、nullでない場合のみ、プロファイルカバーをロードするために、それが発生しますが、それは、その後であれば、デフォルトのプロフィール画像が使用されます。

1

のListViewItemのためのあなたの生成されたレイアウトは、画像

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

を持っているとのListViewItemが作成されたとき、あなたはすぐに画像を上書きイメージビュー

@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); 
    ... 

そのため、あなたはK3Bとして、デフォルトの画像

関連する問題