Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created March 13, 2022 11:45
Show Gist options
  • Save codeforfun-jp/163296db7953d4543edc57cf7d01ebec to your computer and use it in GitHub Desktop.
Save codeforfun-jp/163296db7953d4543edc57cf7d01ebec to your computer and use it in GitHub Desktop.
【Kotlin】ListView with imageview and textview 3
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// データを用意
val data = listOf(
mapOf ("image" to R.drawable.bird, "name" to "とり", "detail" to "「とり」の説明文が入ります。"),
mapOf ("image" to R.drawable.deer, "name" to "しか", "detail" to "「しか」の説明文が入ります。"),
mapOf ("image" to R.drawable.elephant, "name" to "ぞう", "detail" to "「ぞう」の説明文が入ります。"),
mapOf ("image" to R.drawable.fox, "name" to "きつね", "detail" to "「きつね」の説明文が入ります。"),
mapOf ("image" to R.drawable.hippo, "name" to "かば", "detail" to "「かば」の説明文が入ります。"),
mapOf ("image" to R.drawable.lion, "name" to "ライオン", "detail" to "「ライオン」の説明文が入ります。"),
mapOf ("image" to R.drawable.panda, "name" to "パンダ", "detail" to "「パンダ」の説明文が入ります。"),
mapOf ("image" to R.drawable.sheep, "name" to "ひつじ", "detail" to "「ひつじ」の説明文が入ります。"),
)
// ListViewにデータをセットする
val list = findViewById<ListView>(R.id.list)
list.adapter = SimpleAdapter(
this,
data,
R.layout.list_item,
arrayOf("image", "name", "detail"),
intArrayOf(R.id.image, R.id.name, R.id.detail)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment