Android:使用数组项填充列表视图 [英] Android: Populating a listview with array items
问题描述
我是 Android 新手,我想我正在尝试做一些非常基本的事情:我的数组中有 5 个字符串(比如一"、二"……).我想将这 5 个字符串添加到我的 listactivity 中的列表视图中.
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 list view in my listactivity.
我的清单:
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
我的列表行:
<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>
基本上,我想将 Array 项绑定到 TextView homeItemName.稍后我可能会在我的行中添加其他项目,所以我不能只是将列表视图绑定到条目.
Basically, I want to bind the Array items to the TextView homeItemName. I might add other items in my row later, so I can't just bind the listview to the entries.
谢谢!
推荐答案
对于代码,快速浏览一下这个分步教程
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();
它显示了 ArrayAdapter 的基本实现:
It shows a basic implementation of an ArrayAdapter:
R.layout.list_item :是将用于您的每个 ROW 的 xml 布局 (list_item.xml)列表显示.COUNTRIES 是字符串数组.
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.
这篇关于Android:使用数组项填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!