2013-07-11 114 views
2

我完全陌生于android编程,我正尝试创建一个friendslist类。用ArrayAdapter挣扎

首先,我做的是从数据库加载对象的数组:

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。

回答

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); 

希望这有助于了解的基础知识。关于这个主题,这是一个不错的tutorial

+0

非常感谢您的建设性答案,我会竖起大拇指,但显然我缺乏声誉! – user2573715

+0

答案的答案并不是最后一个答案,其他人,看看你的问题,可以考虑如何正确解决问题。 –