Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created March 13, 2022 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/b5f1bb33413e5583213b72ef72249d54 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/b5f1bb33413e5583213b72ef72249d54 to your computer and use it in GitHub Desktop.
【Kotlin】ListView with two textview 3
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// データを用意
val data = mutableListOf(
mapOf ("name" to "とり", "detail" to "「とり」の説明文が入ります。"),
mapOf ("name" to "しか", "detail" to "「しか」の説明文が入ります。"),
mapOf ("name" to "ぞう", "detail" to "「ぞう」の説明文が入ります。"),
mapOf ("name" to "きつね", "detail" to "「きつね」の説明文が入ります。"),
mapOf ("name" to "かば", "detail" to "「かば」の説明文が入ります。"),
mapOf ("name" to "ライオン", "detail" to "「ライオン」の説明文が入ります。"),
mapOf ("name" to "パンダ", "detail" to "「パンダ」の説明文が入ります。"),
mapOf ("name" to "ひつじ", "detail" to "「ひつじ」の説明文が入ります。"),
)
// ListViewにデータをセットする
val list = findViewById<ListView>(R.id.list)
list.adapter = SimpleAdapter(
this,
data,
android.R.layout.simple_list_item_2,
arrayOf("name", "detail"),
intArrayOf(android.R.id.text1, android.R.id.text2)
)
// クリックイベント
list.setOnItemClickListener { adapterView, view, position, id ->
val message = "「${data[position]["name"]}」をクリックしました。"
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