2012-05-10 51 views
1

所以我有我的自定義XML文件,這是我爲它做的這個listview。Android設置listView列表中的圖像

在文件中,有一個沒有源集的圖像具有固定的ID。

我想將此圖片源更改爲可繪製文件夾中的某個資源。

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 
<ImageView 
android:id="@+id/listImage" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:contentDescription="protection image" 
android:layout_marginTop="10dp" 
/> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_marginLeft="10dp" 
> 
<TextView android:id="@+id/text1" 
android:textSize="20dp" 
android:textStyle="bold" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
<TextView android:id="@+id/text2" 
android:textSize="12dp" 
android:layout_marginTop="5dp" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent"/> 
</LinearLayout> 
</LinearLayout> 

</LinearLayout> 

這是我迄今爲止嘗試過了,它崩潰:

 adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current"}, new int[] {R.id.text1, R.id.text2}); 
    ImageView img; 
    img = (ImageView) findViewById(R.id.listImage); 
    Drawable d = getResources().getDrawable(R.drawable.opened); 
    img.setImageDrawable(d); 
    setListAdapter(adapter); 
+0

做ü想設置相同的圖像全部列表項目,或不同的圖像列表的每個項目? – KMI

+0

我有兩個圖像,我需要決定哪些項目得到什麼圖像 – arielschon12

+0

什麼是您的條件來決定列表項目的圖像? –

回答

1
ImageView img; 
    img = (ImageView) findViewById(R.id.listImage); 

這意味着你是指一個物體是出現在佈局中,您可以將其用於活動的setContentView()。但您的ImageView不在該佈局中。

在這裏你試圖引用這個ListView。所以顯然你得到了空指針異常。

您必須使用自定義適配器以及。嘗試從下面的鏈接。

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

http://www.mkyong.com/android/android-listview-example/

+0

林不知道我明白這些例子..是否有任何簡單的方法來改變圖像,如果我有ID? – arielschon12

+0

不可以。自定義列表視圖不可用。您只能使用自定義適配器。 –

+0

那麼,但我的listview有兩行。是否有可能適應這一點呢? – arielschon12

相關問題