关于Android中的BaseAdapter


写个小程序时候用到了BaseAdapter,继承它后用来适配GridView。(应该也可以适配其它的View)

继承后没修改时候,有几个override的方法。

1。getCount(),返回值为int类型的。

这个函数的主要作用是为GridView提供显示网格内容数量。如果做的是一个照片浏览的话,这个返回值就是在屏幕上显示照片的数量。

2。getItem(int position),返回值为Object类型,默认返回null。

这个函数的主要作用尚未弄清楚。其是从Adapter这个接口继承而来。官方解释是Get the data item associated with the specified position in the data set.即获得相应数据集合中特定位置的数据项。似乎默认不会被调用。

3。getItemId(int position),返回值类型为long,默认返回0。

文档解释为:Get the row id associated with the specified position in the list.但在打印时候,似乎position一直为0。但默认会被调用,好像是初始化时候被调用。

4。getView(int position, View convertView, ViewGroup parent),返回View类型。

这个是进行适配的主要方法。返回的view会显示在GridView上。返回的数量与前面的getCount函数返回值是一样的。

相关内容