Android: Populating a listview with array items

后端 未结 2 799
我在风中等你
我在风中等你 2020-12-03 04:59

I\'m new to Android and I think I\'m trying to do something really basic: I have a 5 strings in my Array (say \'One\', \'Two\', ...). I want to add these 5 strings to my l

相关标签:
2条回答
  • 2020-12-03 05:10

    You can use an ArrayAdapter to bind your data. Since you want to be able to add extra data items to the view, you to give the adapter an ArrayList (since an array is of a fixed size). Items should be added via the ArrayAdapter and your ArrayList will be updated automatically. I have an example at http://www.box.net/shared/yduel9txya

    0 讨论(0)
  • 2020-12-03 05:27

    For code, take a quick look at this step-by-step tutorial

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));  
    ListView lv = getListView();
    

    It shows a basic implementation of an ArrayAdapter:

    R.layout.list_item : is the xml layout (list_item.xml) that will be used for every ROW of your listview. COUNTRIES is the array of Strings.

    0 讨论(0)
提交回复
热议问题