setListAdapter究竟做了什么?

时间:2014-05-12 20:50:44

标签: android

我正在调试一个我没有写过的Android程序,所以这很多都试图弄清楚它是如何工作的。

它有一个ListActivity,ListActivity的一个成员是一个名为lv的ListView。我正在尝试跟踪lv如何在 onCreate()中初始化时填充。我在调试器中添加了几行来查看。 。 。

mylistadapter = new MyListAdapter(myListActivity.this);
int lvCount = lv.getCount();  // for debugging only - delete before releasing
setListAdapter(mylistadapter);
lvCount = lv.getCount();  // for debugging only - delete before releasing

...而且我可以看到lvCount在调用 setListAdapter()之前为0,然后在调用之后为15。

但Google的文档只是说,

  

public void setListAdapter (ListAdapter适配器)

     

提供列表视图的光标。

这对我说的并不是很重要。有人可以更详细地解释这意味着什么以及它如何解释我所看到的结果?

其他信息

mylistadapter = new MyListAdapter(MyListActivity.this);

   protected class MyListAdapter extends BaseAdapter 
   . . . 

1 个答案:

答案 0 :(得分:0)

ListView从MyListAdapter对象获取其计数。 MyListAdapter应该在某处实现getCount() - 方法。这就是15号来自哪里。在设置适配器之前它是0,因为没有适配器。这有帮助吗?

编辑: 下面是ArrayAdapter文档的链接:
http://developer.android.com/reference/android/widget/ArrayAdapter.html
我的猜测是MyListAdapter继承自ArrayAdapter,但如果它不让我知道。

回应其他信息:
下面是Adapter-Docs的链接:
http://developer.android.com/reference/android/widget/Adapter.html
多数派MyListAdapter继承了getCount()方法。它是一个在BaseAdapter类中实现的接口。

回应评论: 根据文档,lv.getCount()返回

The number of items owned by the Adapter associated with this AdapterView. (This is the number of data items, which may be larger than the number of visible views.)   

所以基本上只调用adpaters getCount(),如果没有适配器则返回0 (ListView从AdapterView继承getCount()) 链接到文档:
http://developer.android.com/reference/android/widget/AdapterView.html#getCount%28%29