Android - 如何将 ListView 绑定到自定义对象集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2318313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How do you bind a ListView to a collection of custom objects?
提问by Tristan Warner-Smith
I've got a class called PhoneContact
with 3 properties:
Title, Descriptionand PhoneNumber
我有一个名为PhoneContact
3 个属性的类:
Title、Description和PhoneNumber
If I have a collection of these, how would I go about binding these to a ListView
containing a TextView
for each property?
如果我有这些集合,我将如何将这些绑定到ListView
包含TextView
每个属性的 a ?
回答by Eric Burke
Extend ArrayAdapter
, overriding the getView(int, View, ViewGroup)
.
扩展ArrayAdapter
,覆盖getView(int, View, ViewGroup)
.
To hook up your custom adapter, I'll assume your Activity
extends ListActivity
. In one of the lifecycle methods (such as onCreate()
, onResume()
, or onStart()
), call setListAdapter(myCustomAdapter)
.
为了连接您的自定义适配器,我将假设您的Activity
extends ListActivity
。在生命周期方法中的一种(例如onCreate()
,onResume()
或onStart()
),呼叫setListAdapter(myCustomAdapter)
。
This article shows how to do it: Android Series: Custom ListView items and adapters.
本文展示了如何做:Android 系列:自定义 ListView 项和适配器。