2012-06-11 159 views
1

我已經創建了一個將文本傳遞給SimpleAdapter的動態列表視圖,但是我想傳遞信息以使圖像顯示在可繪製文件夾中。這是每一行的XML佈局:Android將圖像添加到動態列表視圖

<ImageView 
    android:id="@+id/sport_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />  
<TextView 
    android:id="@+id/item_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:padding="2dp" 
    android:textSize="20dp" /> 
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" > 
<TextView 
    android:id="@+id/item_subtitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:textSize="13dp" /> 
</LinearLayout> 
</LinearLayout> 

和代碼創建列表位:目前

//fill in the list items from the XML document 
    for (int i = 0; i < nodes.getLength(); i++) {       
     HashMap<String, String> map = new HashMap<String, String>();  

     Element e = (Element)nodes.item(i); 
     map.put("id", XMLfunctions.getValue(e, "fixture_id")); 
     map.put("sport", XMLfunctions.getValue(e, "sport")); 
     map.put("teams", XMLfunctions.getValue(e, "team_2") + " v " + XMLfunctions.getValue(e, "team_2")); 
     mylist.add(map);    
    }  

    //Make a new listadapter 
    ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.fixture, new String[] { "sport", "teams" }, new int[] { R.id.item_title, R.id.item_subtitle }); 

    setListAdapter(adapter); 

現在,當我通過字符串運動,它只是在item_title文本視圖中顯示諸如「足球」之類的文本。但是,因爲我知道這項運動是什麼,我希望能夠在sport_icon imageview中顯示圖標。體育圖標位置的例子是@ drawable/icon_football。

非常感謝。


答案在這裏被創造使用ListView一個BaseAdapter由弗蘭克以下建議。如果其他人想要做同樣的事情,但不知道從哪裏開始,我強烈建議您觀看http://www.youtube.com/watch?v=pVs4qKmenQM。這段視頻融合在一起,解釋了你需要知道的一切。

:)

回答