-
-
Save codeforfun-jp/70dab9ef25a6cc5cb5c9e40a54239554 to your computer and use it in GitHub Desktop.
【Kotlin】ListView with two textview 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
val adapter = SimpleAdapter( | |
this, | |
data, | |
android.R.layout.simple_list_item_2, | |
arrayOf("name", "detail"), | |
intArrayOf(android.R.id.text1, android.R.id.text2) | |
) | |
list.adapter = adapter | |
// クリックイベント | |
list.setOnItemClickListener { adapterView, view, position, id -> | |
// 新しい項目を追加 | |
data.add(mapOf("name" to "いぬ", "detail" to "「いぬ」の説明文が入ります。")) | |
// クリックした項目を削除 | |
data.removeAt(position) | |
// 変更を反映させる | |
adapter.notifyDataSetChanged() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment