kotlin

リストビューを表示するためにListViewAdapterを実装する

更新日:

ListViewAdapter

リストビューを表示するためにListViewAdapterを実装する。
BaseAdapterを継承して必要なメソッドを実装する。
実装が必要なのは以下。

  • fun getCount(): Int
  • fun getItem(position: Int): Any?
  • fun getItemId(position: Int): Long
  • fun getView(position:Int,convertView: View?,parent: ViewGroup?): View

fun getCount(): Int

名前の通り、リストビューが保持するアイテムの個数を返す。
ListViewAdapterが保持するList<Article&gtのサイズを返すようにする。

fun getItem(position: Int): Any?

名前の通り、指定した位置のアイテムを返す。
保持するList<Article&gtの要素を返すようにする。

fun getItemId(position: Int): Long

指定した位置のアイテムに関するアプリケーション固有のIDを返す。
ListViewの外から「位置->固有ID」を取得できる。
アイテムを取得してアイテムからIDを取るんじゃダメなのか…。

Get the row id associated with the specified position in the list.

fun getView(position:Int,convertView: View?,parent: ViewGroup?): View

指定した位置のアイテムを表示するためのビューを取得する。
convertViewには、画面から表示しきれなくなったViewが来る。
既に画面に表示されているならばnullが来る。
画面から表示しきれなくなったView(convertView)を使い回すことで負荷削減する。

Get a View that displays the data at the specified position in the data set.

convertViewをArticleViewにダウンキャストした結果をViewとする。
ダウンキャストがnullならばエルビス演算子(?:)の右辺が評価され、新しいArticleViewが作られる。

-kotlin
-

Copyright© ikuty.com , 2024 AllRights Reserved Powered by AFFINGER4.