Android:使用数组项填充列表视图

Pra*_*bhu 28 java arrays android listview

我是Android的新手,我想我正在尝试做一些非常基本的事情:我的数组中有5个字符串(比如'One','Two',......).我想在listactivity中将这5个字符串添加到列表视图中.

我的列表:

<ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的列表行:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<TextView android:id="@+id/homeItemName" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

基本上,我想将Array项绑定到TextView homeItemName.我可能稍后在我的行中添加其他项目,所以我不能只将listview绑定到条目.

谢谢!

Hub*_*ert 31

有关代码,请快速查看此分步教程

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

它显示了ArrayAdapter的基本实现:

R.layout.list_item:是用于列表视图的每个ROW的xml布局(list_item.xml).COUNTRIES是一系列字符串.


Kla*_*rth 6

您可以使用ArrayAdapter绑定数据.由于您希望能够向视图添加额外的数据项,因此您需要为适配器提供ArrayList(因为数组具有固定大小).应该通过ArrayAdapter添加项目,并且您的ArrayList将自动更新.我在http://www.box.net/shared/yduel9txya上有一个例子