LoginSignup
0
0

More than 5 years have passed since last update.

Android: ListViewの使い方

Last updated at Posted at 2017-10-13

Android studio 2.3.3 でListViewを使おうとしたときに様々な情報に混乱したので、今後迷わないようにメモ書きを残しておきます。
ListViewを使うときにはリソースファイルで文字列リストを定義しておいて、IDで参照するということだったのですが、次のようにすると失敗しました…。

失敗例

string.xml
<resources>
    <string-array name="spice">
        <item>田中</item>
        <item>山崎</item>
        <item>佐藤</item>
    </string-array>
</resources>

リソースファイルとしてstring.xmlから参照してもだめなようでした。
別にarray用のファイルを用意するとうまくいきました。

成功例

array.xml
<resources>
    <string-array name="namae">
        <item>田中</item>
        <item>山崎</item>
        <item>佐藤</item>
    </string-array>
</resources>
activity_main.xml
<ListView
        android:layout_width="0dp"
        android:layout_height="495dp"
        android:entries="namae"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_editor_absoluteY="8dp" />

これでうまくいくはず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0