2012-07-26 32 views
-1

我有这个ListView控件+资源>图片+文字

setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(), 
       R.array.listOfCountries, R.layout.row)); 

,这让从这个资源XML

<string-array name="listOfCountries"> 
     <item>Bulgaria</item> 
     <item>Canada</item> 
     <item>Croatia</item> 
     <item>USA</item> 
     <item>UK</item> 
    </string-array> 

基本列表视图,但我需要添加当前图像TU每一个国家,例如到保加利亚我想添加布拉格里亚的旗帜等等。

但我需要在资源中存储此字符串数组,因为我想为更多语言进行本地化。

谢谢

+0

以下教程将帮助您满足您的需求** [Custom ListView](http://www.ezzylearning.com/tutorial.aspx?tid=1763429)和[this](http://android-er.blogspot。 co.uk/2010/06/custom-arrayadapter-with-with-different.html)**视频 - [youtubeVideo](http://www.youtube.com/watch?v=LjAlNfa6obU)希望它有帮助 – GoCrazy 2012-07-26 17:36:26

回答

1

你将不得不通过继承BaseAdapter并重写getView(创建自己的ListAdapter),getCount将(),getItem()时,等方法。

http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/

创建一个名为国家像这个类:

public class Country { 
    public String name; 
    public int flagResourceId; 

    public Country(String name, int flagResourceId) { 
     this.name = name; 
     this.flagResourceId = flagResourceId; 
    } 
} 

然后你可以使用在您的自定义BaseAdapter,而该对象到视图中getView绑定()。创建列表的过程可以在onCreate()期间完成,也可以在应用程序的类中完成。