Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Last active March 13, 2022 11:11
Show Gist options
  • Save codeforfun-jp/1d632b4ad7b42514096f42f636f9772c to your computer and use it in GitHub Desktop.
Save codeforfun-jp/1d632b4ad7b42514096f42f636f9772c to your computer and use it in GitHub Desktop.
【Kotlin】ListView with one textview 5
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// データを用意
val data = listOf(
"とり", "しか", "ぞう", "きつね",
"かば", "ライオン", "パンダ", "ひつじ"
)
// ListViewにデータをセットする
val list = findViewById<ListView>(R.id.list)
list.adapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
data
)
// クリックイベント
list.setOnItemClickListener { adapterView, view, position, id ->
val message = "「${(data[position])}」をクリックしました。"
Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment