ListViewの使い方
ListViewとは
ListViewは複数のデータを表示するのに使いやすい部品です。
リスト項目は、リソースからあるいは、プログラムから生成することができます。
リソースとして配列を作成し、それをListViewに表示するだけなら
Javaのコードを書かなくともできますが、項目を変更することができません。
項目を変更する必要がある場合には、ArrayAdapterを使い、Javaのコードを書くことになります。
この方法はこちらご覧ください。
つぎのような画面を作りたい時に、各項目をstring-arrayとしてリソースフィルに作成し、activity_mainには、
ListViewを1つ作成し、android:entriesでstring-arrayのnameを指定します。
strings.xmlの例(リスト項目の配列を作成)
<resources>
<string name="app_name">>ListViewArray1</string>
<string-array name="menu">
<item>ハンバーグ</item>
<item>ステーキ</item>
<item>カレーライス</item>
<item>オムライス</item>
<item>ナポリタン</item>
<item>カルボナーラ</item>
<item>ペスカトーレ</item>
<item>グリーンサラダ</item>
</string-array>
</resources<
activity_main.xmlの例
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="0dp"
android:layout_height="0dp"
android:entries="@array/menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>