2012-06-11 162 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。这段视频融合在一起,解释了你需要知道的一切。

:)

回答