2

我对 android 编程完全陌生,我正在尝试创建一个朋友列表类。

首先,我创建了一个从数据库加载对象的数组:

friendArray = new Friend[NumberOfFriendsInDatabase];

//gets friend objects from the database, and loads them into an array
for (int i=0;i<NumberOfFriendsInDatabase;i++){
    friendArray[i]=new Friend("","","");//swap this with an object or data from database handler.

然后我创建一个列表视图,我希望在其中直观地表示数组:

friendListView  =(ListView) findViewById(R.id.FriendsListView1);

最后,我明白我需要使用适配器来实现这一点,这就是我感到困惑的地方。

friendListAdapter = ArrayAdapter();

我在创建适配器时遇到问题,而且我无法理解官方文档。我想要的只是适配器中的friendArray,我可以与listView一起使用。

4

1 回答 1

2

您可以像这样使用 ArrayAdapter:

// A Collection which holds your values
List<YourFriendObject> list = new ArrayList<YourFriendObject>();

// fill the Collection with your data
// you should use for-each but I dont know your object
for (int i = 0; i < friendArray.length; i++)
  list.add(friendArray[i]);

// The ArrayAdapter takes a layout, in this case a standard one
// and the collection with your data
ArrayAdapter<YourFriendObject> adapter = new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, list);

 // provide the adapter to your listview
frienListView.setAdapter(adapter);

希望这有助于理解基础知识。这是关于这个主题的一个很好的教程。

于 2013-07-11T17:52:16.140 回答