0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android】KotlinでRoomデータベースとListViewを繋げてメモ帳を作成

Last updated at Posted at 2020-12-31

####関連
前回記事:
KotlinでRoomデータベースを使用
https://qiita.com/clipbord/items/c7a12ec17412b10578fd

全ソース:
https://github.com/CoffCookie/memoList

####参考
https://techacademy.jp/magazine/32252
上記を参考に上記記事で作成したコードにListViewを組み込んでみた。

###ListViewのコード

MainActivity.kt
        val data = arrayListOf("0")

        //配列にデータベースに保存したデータを取り出して保存
        for (memo in memoList) {
            data.add(memo.text.toString())
        }

        // リスト項目とListViewを対応付けるArrayAdapterを用意する
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,data)
        // ListViewにArrayAdapterを設定する
        val listView: ListView = findViewById(R.id.listView)
        listView.adapter = adapter

###View作成

activity_main.xml
    <EditText
        android:id="@+id/memoText"
        android:layout_width="270dp"
        android:layout_height="101dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:inputType="textMultiLine|textPersonName"
        android:text="@string/memo_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:text="@string/save_button"
        app:layout_constraintBottom_toBottomOf="@+id/memoText"
        app:layout_constraintStart_toEndOf="@+id/memoText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="321dp"
        android:layout_height="407dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="@+id/memoText"
        app:layout_constraintTop_toBottomOf="@+id/memoText" />
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?