質問編集履歴
12
質問内容を変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Re
|
1
|
+
Realmのgradleの記述はそもそもこれで正しいのでしょうか?
|
test
CHANGED
@@ -1,43 +1,56 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
|
-
kotlinで
|
3
|
+
kotlinで書籍を見ながらRealmの勉強中。
|
4
|
-
参考サイトはこちら
|
5
|
-
|
4
|
+
ただ、書籍のコード自体が古くてそのまま書いてもエラーになるし
|
6
|
-
|
7
|
-
|
5
|
+
ネットで調べても、みなさん書き方がまちまちなので、修正しまくっているうちに
|
8
|
-
|
6
|
+
今自分は、何がとりあえず正解なのかどうかさえ、わからなくなった。
|
9
7
|
|
10
8
|
### 実現したいこと
|
11
|
-
他のほとんどのサイトではdependenciesに下記コードを追加するよう説明があります
|
12
|
-
implementation 'androidx.recyclerview:recyclerview:1.1.0'
|
13
|
-
|
14
|
-
今回は、記述しなくても出来てしまいました。
|
15
|
-
これはどういう意味なのか。
|
16
|
-
「今は記述しなくてよくなった(昔は必要だった)」という意味なのか、あるいは
|
17
|
-
「何かしらの条件下においては必要だが、今回のケースでは不要」という意味なのか、
|
18
|
-
理解できない。
|
19
|
-
|
9
|
+
Realmのgradleの追記はそもそもこれで正しいのかどうかの検証をお願いしたい。
|
20
10
|
|
21
11
|
### 発生している問題・エラーメッセージ
|
22
12
|
|
23
13
|
```
|
14
|
+
メモ帳アプリのようなものを作って、「保存ボタン」を押した時に
|
24
|
-
|
15
|
+
エミュレータがクラッシュして閉じてしまう。
|
16
|
+
いくつかエラーは考えられるので順次対応しなければいけないが、
|
17
|
+
そもそもの第一歩として、Realmの記述の正解がわからないので
|
25
|
-
|
18
|
+
※とにかくどの解説サイトもちょっとずつみなさん違う(人、時期によって)。これがとにかく初学者には混乱する
|
26
19
|
|
27
20
|
```
|
28
21
|
|
29
22
|
### 該当のソースコード
|
23
|
+
```build.gradle(プロジェクト)
|
24
|
+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
25
|
+
buildscript {
|
26
|
+
dependencies {
|
27
|
+
classpath "io.realm:realm-gradle-plugin:10.8.0"
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
plugins {
|
32
|
+
id 'com.android.application' version '7.2.0' apply false
|
33
|
+
id 'com.android.library' version '7.2.0' apply false
|
34
|
+
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
|
35
|
+
}
|
36
|
+
|
37
|
+
task clean(type: Delete) {
|
38
|
+
delete rootProject.buildDir
|
39
|
+
}
|
40
|
+
```
|
30
41
|
```build.gradle(app)
|
31
42
|
plugins {
|
32
43
|
id 'com.android.application'
|
33
44
|
id 'org.jetbrains.kotlin.android'
|
45
|
+
id 'kotlin-kapt'
|
46
|
+
id 'realm-android'
|
34
47
|
}
|
35
48
|
|
36
49
|
android {
|
37
50
|
compileSdk 32
|
38
51
|
|
39
52
|
defaultConfig {
|
40
|
-
applicationId "com.example.samplere
|
53
|
+
applicationId "com.example.samplerealm"
|
41
54
|
minSdk 21
|
42
55
|
targetSdk 32
|
43
56
|
versionCode 1
|
@@ -59,118 +72,32 @@
|
|
59
72
|
kotlinOptions {
|
60
73
|
jvmTarget = '1.8'
|
61
74
|
}
|
75
|
+
buildFeatures {
|
76
|
+
viewBinding true
|
77
|
+
}
|
62
78
|
}
|
63
79
|
|
64
80
|
dependencies {
|
65
81
|
|
82
|
+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
|
66
83
|
implementation 'androidx.core:core-ktx:1.7.0'
|
67
84
|
implementation 'androidx.appcompat:appcompat:1.4.1'
|
68
85
|
implementation 'com.google.android.material:material:1.6.0'
|
69
86
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
87
|
+
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
|
88
|
+
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
|
70
89
|
testImplementation 'junit:junit:4.13.2'
|
71
90
|
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
72
91
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
73
92
|
}
|
74
93
|
```
|
75
|
-
```item_recycler_list.xml
|
76
|
-
<?xml version="1.0" encoding="utf-8"?>
|
77
|
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
78
|
-
android:layout_width="match_parent"
|
79
|
-
android:layout_height="wrap_content"
|
80
|
-
android:orientation="vertical"
|
81
|
-
android:padding="10dp">
|
82
|
-
|
83
|
-
<LinearLayout
|
84
|
-
android:layout_width="match_parent"
|
85
|
-
android:layout_height="54dp"
|
86
|
-
android:layout_marginLeft="10dp"
|
87
|
-
android:layout_marginRight="10dp"
|
88
|
-
android:layout_marginBottom="0dp">
|
89
|
-
|
90
|
-
|
94
|
+
### 試したこと・その他
|
91
|
-
android:id="@+id/Text1"
|
92
|
-
android:layout_width="wrap_content"
|
93
|
-
android:layout_height="wrap_content"
|
94
|
-
android:layout_marginTop="8dp"
|
95
|
-
android:textSize="30sp" />
|
96
|
-
|
97
|
-
</LinearLayout>
|
98
|
-
<View
|
99
|
-
android:id="@+id/divider"
|
100
|
-
android:layout_width="match_parent"
|
101
|
-
android:layout_height="1dp"
|
102
|
-
android:layout_weight="1"
|
103
|
-
android:background="?android:attr/listDivider" />
|
104
|
-
|
105
|
-
</LinearLayout>
|
106
|
-
```
|
107
|
-
```ViewHolderList.kt
|
108
|
-
package com.example.samplerecyclerview
|
109
|
-
|
110
|
-
import android.view.View
|
111
|
-
import android.widget.TextView
|
112
|
-
import androidx.recyclerview.widget.RecyclerView
|
113
|
-
|
114
|
-
class ViewHolderList(item: View) :RecyclerView.ViewHolder(item) {
|
115
|
-
val characterList : TextView = item.findViewById(R.id.Text1)
|
116
|
-
}
|
117
|
-
```
|
118
|
-
|
95
|
+
解説サイトによっては、上記2つじゃなくって
|
119
|
-
package com.example.samplerecyclerview
|
120
|
-
|
121
|
-
import android.view.LayoutInflater
|
122
|
-
import android.view.ViewGroup
|
123
|
-
import androidx.recyclerview.widget.RecyclerView
|
124
|
-
|
125
|
-
class RecyclerAdapter(val list: List<String>) :RecyclerView.Adapter<ViewHolderList>() {
|
126
|
-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderList {
|
127
|
-
//TODO("Not yet implemented")
|
128
|
-
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_recycler_list, parent, false)
|
129
|
-
return ViewHolderList(itemView)
|
130
|
-
}
|
131
|
-
|
132
|
-
override fun onBindViewHolder(holder: ViewHolderList, position: Int) {
|
133
|
-
//TODO("Not yet implemented")
|
134
|
-
holder.characterList.text = list[position]
|
135
|
-
}
|
136
|
-
override fun getItemCount(): Int = list.size
|
137
|
-
}
|
138
|
-
```
|
139
|
-
```MainActivity.kt
|
140
|
-
package com.example.samplerecyclerview
|
141
|
-
|
142
|
-
import androidx.appcompat.app.AppCompatActivity
|
143
|
-
import android.os.Bundle
|
144
|
-
import androidx.recyclerview.widget.LinearLayoutManager
|
145
|
-
import androidx.recyclerview.widget.RecyclerView
|
146
|
-
|
147
|
-
class MainActivity : AppCompatActivity() {
|
148
|
-
private lateinit var recyclerView: RecyclerView
|
149
|
-
|
150
|
-
override fun onCreate(savedInstanceState: Bundle?) {
|
151
|
-
super.onCreate(savedInstanceState)
|
152
|
-
|
96
|
+
「settings.gradle」に記述しなさい、という解説もありました。
|
153
|
-
|
154
|
-
val TextList = listOf(
|
155
|
-
"マリオ",
|
156
|
-
"ルイージ",
|
157
|
-
"ピーチ",
|
158
|
-
"クッパ",
|
159
|
-
"デイジー",
|
160
|
-
"ワリオ",
|
161
|
-
"ワルイージ",
|
162
|
-
"ヘイホー"
|
163
|
-
)
|
164
|
-
|
165
|
-
|
97
|
+
比較的日付の新しい記事だったので「最近はそうしなさい」という意味なのか、
|
166
|
-
|
98
|
+
「たまたま新しく出てきた人が、そういうトリッキーな書き方をしただけ」なのか、も判断できませんで
|
99
|
+
ここにたどり着いた次第です。
|
167
|
-
|
100
|
+
とにもかくにも、上記で正しいのか、間違っているのか。Realmの正解を教えてください。
|
168
|
-
}
|
169
|
-
}
|
170
|
-
```
|
171
|
-
|
172
|
-
|
173
|
-
|
174
101
|
|
175
102
|
### 補足情報(FW/ツールのバージョンなど)
|
176
103
|
|
11
補足追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,7 +13,7 @@
|
|
13
13
|
|
14
14
|
今回は、記述しなくても出来てしまいました。
|
15
15
|
これはどういう意味なのか。
|
16
|
-
「今は記述しなくてよくなった」のか、あるいは
|
16
|
+
「今は記述しなくてよくなった(昔は必要だった)」という意味なのか、あるいは
|
17
17
|
「何かしらの条件下においては必要だが、今回のケースでは不要」という意味なのか、
|
18
18
|
理解できない。
|
19
19
|
なので「何故dependenciesを追加しなくても出来てしまったのか」解説してほしい。
|
10
補足追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,7 @@
|
|
16
16
|
「今は記述しなくてよくなった」のか、あるいは
|
17
17
|
「何かしらの条件下においては必要だが、今回のケースでは不要」という意味なのか、
|
18
18
|
理解できない。
|
19
|
+
なので「何故dependenciesを追加しなくても出来てしまったのか」解説してほしい。
|
19
20
|
|
20
21
|
### 発生している問題・エラーメッセージ
|
21
22
|
|
9
不要文言削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -169,12 +169,7 @@
|
|
169
169
|
```
|
170
170
|
|
171
171
|
|
172
|
-
### 試したこと
|
173
172
|
|
174
|
-
realm= Realm.getDefaultInstance()
|
175
|
-
の一文を消すと、エミュレータは正常に起動する。
|
176
|
-
ただし、realm= Realm.getDefaultInstance()を消していいのか、
|
177
|
-
あるいは(書籍が古いため)別の書き方にしないといけないのか、判断できないため質問させていただきました。
|
178
173
|
|
179
174
|
### 補足情報(FW/ツールのバージョンなど)
|
180
175
|
|
8
コード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,51 @@
|
|
26
26
|
```
|
27
27
|
|
28
28
|
### 該当のソースコード
|
29
|
+
```build.gradle(app)
|
30
|
+
plugins {
|
31
|
+
id 'com.android.application'
|
32
|
+
id 'org.jetbrains.kotlin.android'
|
33
|
+
}
|
34
|
+
|
35
|
+
android {
|
36
|
+
compileSdk 32
|
37
|
+
|
38
|
+
defaultConfig {
|
39
|
+
applicationId "com.example.samplerecyclerview"
|
40
|
+
minSdk 21
|
41
|
+
targetSdk 32
|
42
|
+
versionCode 1
|
43
|
+
versionName "1.0"
|
44
|
+
|
45
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
46
|
+
}
|
47
|
+
|
48
|
+
buildTypes {
|
49
|
+
release {
|
50
|
+
minifyEnabled false
|
51
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
52
|
+
}
|
53
|
+
}
|
54
|
+
compileOptions {
|
55
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
56
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
57
|
+
}
|
58
|
+
kotlinOptions {
|
59
|
+
jvmTarget = '1.8'
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
dependencies {
|
64
|
+
|
65
|
+
implementation 'androidx.core:core-ktx:1.7.0'
|
66
|
+
implementation 'androidx.appcompat:appcompat:1.4.1'
|
67
|
+
implementation 'com.google.android.material:material:1.6.0'
|
68
|
+
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
69
|
+
testImplementation 'junit:junit:4.13.2'
|
70
|
+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
71
|
+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
72
|
+
}
|
73
|
+
```
|
29
74
|
```item_recycler_list.xml
|
30
75
|
<?xml version="1.0" encoding="utf-8"?>
|
31
76
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
7
質問内容を変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
RecyclerViewにはdependenciesを追加する必要があるのか、無いのかについて
|
test
CHANGED
@@ -1,79 +1,124 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
|
-
|
3
|
+
kotlinでのRecyclerView学習中。
|
4
|
+
参考サイトはこちら
|
5
|
+
https://qiita.com/soutominamimura/items/47a48e4e6e1aff3d3396
|
6
|
+
|
7
|
+
ここを参考にする限り、他のサイトで言っている「dependenciesを追加」とやらをしなくても、
|
4
|
-
|
8
|
+
実装できてしまった。
|
5
9
|
|
6
10
|
### 実現したいこと
|
11
|
+
他のほとんどのサイトではdependenciesに下記コードを追加するよう説明があります
|
12
|
+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
|
7
13
|
|
8
|
-
|
14
|
+
今回は、記述しなくても出来てしまいました。
|
15
|
+
これはどういう意味なのか。
|
16
|
+
「今は記述しなくてよくなった」のか、あるいは
|
17
|
+
「何かしらの条件下においては必要だが、今回のケースでは不要」という意味なのか、
|
18
|
+
理解できない。
|
9
19
|
|
10
20
|
### 発生している問題・エラーメッセージ
|
11
21
|
|
12
22
|
```
|
13
|
-
realm= Realm.getDefaultInstance()
|
14
|
-
|
23
|
+
問題もエラーも発生していませんが
|
15
|
-
|
24
|
+
dependenciesを何も触らなくても、問題もエラーも発生しなかったことがむしろ疑問。
|
25
|
+
|
16
26
|
```
|
17
27
|
|
18
28
|
### 該当のソースコード
|
29
|
+
```item_recycler_list.xml
|
30
|
+
<?xml version="1.0" encoding="utf-8"?>
|
31
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
32
|
+
android:layout_width="match_parent"
|
33
|
+
android:layout_height="wrap_content"
|
34
|
+
android:orientation="vertical"
|
35
|
+
android:padding="10dp">
|
36
|
+
|
37
|
+
<LinearLayout
|
38
|
+
android:layout_width="match_parent"
|
39
|
+
android:layout_height="54dp"
|
40
|
+
android:layout_marginLeft="10dp"
|
41
|
+
android:layout_marginRight="10dp"
|
42
|
+
android:layout_marginBottom="0dp">
|
43
|
+
|
44
|
+
<TextView
|
45
|
+
android:id="@+id/Text1"
|
46
|
+
android:layout_width="wrap_content"
|
47
|
+
android:layout_height="wrap_content"
|
48
|
+
android:layout_marginTop="8dp"
|
49
|
+
android:textSize="30sp" />
|
50
|
+
|
51
|
+
</LinearLayout>
|
52
|
+
<View
|
53
|
+
android:id="@+id/divider"
|
54
|
+
android:layout_width="match_parent"
|
55
|
+
android:layout_height="1dp"
|
56
|
+
android:layout_weight="1"
|
57
|
+
android:background="?android:attr/listDivider" />
|
58
|
+
|
59
|
+
</LinearLayout>
|
60
|
+
```
|
61
|
+
```ViewHolderList.kt
|
62
|
+
package com.example.samplerecyclerview
|
63
|
+
|
64
|
+
import android.view.View
|
65
|
+
import android.widget.TextView
|
66
|
+
import androidx.recyclerview.widget.RecyclerView
|
67
|
+
|
68
|
+
class ViewHolderList(item: View) :RecyclerView.ViewHolder(item) {
|
69
|
+
val characterList : TextView = item.findViewById(R.id.Text1)
|
70
|
+
}
|
71
|
+
```
|
72
|
+
```RecyclerAdapter.kt
|
73
|
+
package com.example.samplerecyclerview
|
74
|
+
|
75
|
+
import android.view.LayoutInflater
|
76
|
+
import android.view.ViewGroup
|
77
|
+
import androidx.recyclerview.widget.RecyclerView
|
78
|
+
|
79
|
+
class RecyclerAdapter(val list: List<String>) :RecyclerView.Adapter<ViewHolderList>() {
|
80
|
+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderList {
|
81
|
+
//TODO("Not yet implemented")
|
82
|
+
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_recycler_list, parent, false)
|
83
|
+
return ViewHolderList(itemView)
|
84
|
+
}
|
85
|
+
|
86
|
+
override fun onBindViewHolder(holder: ViewHolderList, position: Int) {
|
87
|
+
//TODO("Not yet implemented")
|
88
|
+
holder.characterList.text = list[position]
|
89
|
+
}
|
90
|
+
override fun getItemCount(): Int = list.size
|
91
|
+
}
|
92
|
+
```
|
19
|
-
```
|
93
|
+
```MainActivity.kt
|
20
|
-
package com.example.samplere
|
94
|
+
package com.example.samplerecyclerview
|
21
95
|
|
22
96
|
import androidx.appcompat.app.AppCompatActivity
|
23
97
|
import android.os.Bundle
|
24
|
-
import android.widget.
|
98
|
+
import androidx.recyclerview.widget.LinearLayoutManager
|
25
|
-
import android.widget.
|
99
|
+
import androidx.recyclerview.widget.RecyclerView
|
26
|
-
import android.widget.Toast
|
27
|
-
import io.realm.Realm
|
28
|
-
import io.realm.kotlin.createObject
|
29
|
-
import io.realm.kotlin.where
|
30
|
-
import java.util.*
|
31
100
|
|
32
|
-
class
|
101
|
+
class MainActivity : AppCompatActivity() {
|
33
|
-
private val tag = "BloodPressure"
|
34
|
-
private lateinit var re
|
102
|
+
private lateinit var recyclerView: RecyclerView
|
35
103
|
|
36
104
|
override fun onCreate(savedInstanceState: Bundle?) {
|
37
105
|
super.onCreate(savedInstanceState)
|
38
|
-
setContentView(R.layout.activity_
|
106
|
+
setContentView(R.layout.activity_main)
|
39
|
-
val maxEdit:EditText= findViewById(R.id.maxEdit)
|
40
|
-
val minEdit:EditText= findViewById(R.id.minEdit)
|
41
|
-
val pulseEdit: EditText =findViewById(R.id.pulseEdit)
|
42
|
-
val btnSave:Button = findViewById(R.id.btnSave)
|
43
|
-
val btnDelete: Button =findViewById(R.id.btnDelete)
|
44
|
-
realm= Realm.getDefaultInstance()//(※クラッシュ)
|
45
107
|
|
46
|
-
btnSave.setOnClickListener {
|
47
|
-
|
108
|
+
val TextList = listOf(
|
48
|
-
|
109
|
+
"マリオ",
|
49
|
-
|
110
|
+
"ルイージ",
|
111
|
+
"ピーチ",
|
112
|
+
"クッパ",
|
113
|
+
"デイジー",
|
114
|
+
"ワリオ",
|
115
|
+
"ワルイージ",
|
116
|
+
"ヘイホー"
|
117
|
+
)
|
50
118
|
|
51
|
-
if(!maxEdit.text.isNullOrEmpty()){
|
52
|
-
max = maxEdit.text.toString().toLong()
|
53
|
-
}
|
54
|
-
if(!minEdit.text.isNullOrEmpty()){
|
55
|
-
|
119
|
+
recyclerView = findViewById(R.id.RecyclerList)
|
56
|
-
}
|
57
|
-
if(pulseEdit.text.isNullOrEmpty()){
|
58
|
-
pulse=pulseEdit.text.toString().toLong()
|
59
|
-
}
|
60
|
-
|
61
|
-
realm.executeTransaction{
|
62
|
-
val maxId = realm.where<BloodPress>().max("id")
|
63
|
-
val nextId =(maxId?.toLong()?:0L)+1L
|
64
|
-
|
120
|
+
recyclerView.adapter = RecyclerAdapter(TextList)
|
65
|
-
|
121
|
+
recyclerView.layoutManager = LinearLayoutManager(this)
|
66
|
-
bloodPress.max = max
|
67
|
-
bloodPress.mim = min
|
68
|
-
bloodPress.pulse = pulse
|
69
|
-
}
|
70
|
-
Toast.makeText(applicationContext,"保存しました",Toast.LENGTH_LONG).show()
|
71
|
-
finish()
|
72
|
-
}
|
73
|
-
}
|
74
|
-
override fun onDestroy() {
|
75
|
-
super.onDestroy()
|
76
|
-
realm.close()
|
77
122
|
}
|
78
123
|
}
|
79
124
|
```
|
6
補足解説
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,9 @@
|
|
10
10
|
### 発生している問題・エラーメッセージ
|
11
11
|
|
12
12
|
```
|
13
|
+
realm= Realm.getDefaultInstance()
|
13
|
-
|
14
|
+
を記述するとエミュレータがクラッシュする。
|
15
|
+
(消すと、一応正常に起動する)
|
14
16
|
```
|
15
17
|
|
16
18
|
### 該当のソースコード
|
5
要点の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -82,7 +82,7 @@
|
|
82
82
|
realm= Realm.getDefaultInstance()
|
83
83
|
の一文を消すと、エミュレータは正常に起動する。
|
84
84
|
ただし、realm= Realm.getDefaultInstance()を消していいのか、
|
85
|
-
あるいは別の書き方にしないといけないのか、判断できないため質問させていただきました。
|
85
|
+
あるいは(書籍が古いため)別の書き方にしないといけないのか、判断できないため質問させていただきました。
|
86
86
|
|
87
87
|
### 補足情報(FW/ツールのバージョンなど)
|
88
88
|
|
4
要点を絞った
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
「realm= Realm.getDefaultInstance()」の一文を記述するとエミュレータクラッシュしてしまいます。
|
test
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
|
-
RecyclerViewとRelmを使って、入力した情報を表示させるアプリを勉強中。
|
4
|
-
|
3
|
+
書籍でkotlinの学習中。
|
5
|
-
|
4
|
+
Realmでデータを保存する等の処理をしたい
|
6
5
|
|
7
6
|
### 実現したいこと
|
8
7
|
|
9
|
-
保存ボタン(btnSave)が押せるところまで出来たが、その内容が画面に反映されない(真っ白のまま)なので
|
10
|
-
|
8
|
+
エミュレータクラッシュするので、改善したい
|
11
|
-
(※特にbuid_gradleの2つは、そもそもネットで追記するコードがバラバラなので、個人的にはここが一番怪しいと思っています。)
|
12
9
|
|
13
10
|
### 発生している問題・エラーメッセージ
|
14
11
|
|
@@ -17,152 +14,6 @@
|
|
17
14
|
```
|
18
15
|
|
19
16
|
### 該当のソースコード
|
20
|
-
```buid_gradle(project)
|
21
|
-
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
22
|
-
buildscript {
|
23
|
-
dependencies {
|
24
|
-
classpath "io.realm:realm-gradle-plugin:10.8.0"
|
25
|
-
}
|
26
|
-
}
|
27
|
-
|
28
|
-
plugins {
|
29
|
-
id 'com.android.application' version '7.1.3' apply false
|
30
|
-
id 'com.android.library' version '7.1.3' apply false
|
31
|
-
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
|
32
|
-
}
|
33
|
-
|
34
|
-
task clean(type: Delete) {
|
35
|
-
delete rootProject.buildDir
|
36
|
-
}
|
37
|
-
```
|
38
|
-
```buid_gradle(app)
|
39
|
-
plugins {
|
40
|
-
id 'com.android.application'
|
41
|
-
id 'org.jetbrains.kotlin.android'
|
42
|
-
id 'kotlin-kapt'
|
43
|
-
id 'realm-android'
|
44
|
-
}
|
45
|
-
|
46
|
-
android {
|
47
|
-
compileSdk 31
|
48
|
-
|
49
|
-
defaultConfig {
|
50
|
-
applicationId "com.example.samplerealm"
|
51
|
-
minSdk 21
|
52
|
-
targetSdk 31
|
53
|
-
versionCode 1
|
54
|
-
versionName "1.0"
|
55
|
-
|
56
|
-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
57
|
-
}
|
58
|
-
|
59
|
-
buildTypes {
|
60
|
-
release {
|
61
|
-
minifyEnabled false
|
62
|
-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
63
|
-
}
|
64
|
-
}
|
65
|
-
compileOptions {
|
66
|
-
sourceCompatibility JavaVersion.VERSION_1_8
|
67
|
-
targetCompatibility JavaVersion.VERSION_1_8
|
68
|
-
}
|
69
|
-
kotlinOptions {
|
70
|
-
jvmTarget = '1.8'
|
71
|
-
}
|
72
|
-
buildFeatures {
|
73
|
-
viewBinding true
|
74
|
-
}
|
75
|
-
}
|
76
|
-
|
77
|
-
dependencies {
|
78
|
-
|
79
|
-
implementation 'androidx.core:core-ktx:1.7.0'
|
80
|
-
implementation 'androidx.appcompat:appcompat:1.4.1'
|
81
|
-
implementation 'com.google.android.material:material:1.5.0'
|
82
|
-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
83
|
-
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
|
84
|
-
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
|
85
|
-
testImplementation 'junit:junit:4.13.2'
|
86
|
-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
87
|
-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
88
|
-
}
|
89
|
-
```
|
90
|
-
|
91
|
-
```content_main.xml
|
92
|
-
<?xml version="1.0" encoding="utf-8"?>
|
93
|
-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
94
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
95
|
-
xmlns:tools="http://schemas.android.com/tools"
|
96
|
-
android:layout_width="match_parent"
|
97
|
-
android:layout_height="match_parent"
|
98
|
-
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
99
|
-
|
100
|
-
|
101
|
-
<androidx.recyclerview.widget.RecyclerView
|
102
|
-
android:id="@+id/recyclerView"
|
103
|
-
android:layout_width="match_parent"
|
104
|
-
android:layout_height="match_parent"
|
105
|
-
tools:layout_editor_absoluteX="1dp"
|
106
|
-
tools:layout_editor_absoluteY="1dp" />
|
107
|
-
</androidx.constraintlayout.widget.ConstraintLayout>
|
108
|
-
```
|
109
|
-
```MainActivity.kt
|
110
|
-
package com.example.samplerealm
|
111
|
-
|
112
|
-
import android.content.Intent
|
113
|
-
import android.os.Bundle
|
114
|
-
import androidx.appcompat.app.AppCompatActivity
|
115
|
-
import androidx.navigation.ui.AppBarConfiguration
|
116
|
-
import androidx.recyclerview.widget.LinearLayoutManager
|
117
|
-
import androidx.recyclerview.widget.RecyclerView
|
118
|
-
import com.example.samplerealm.databinding.ActivityMainBinding
|
119
|
-
import io.realm.Realm
|
120
|
-
import io.realm.Sort
|
121
|
-
|
122
|
-
class MainActivity : AppCompatActivity() {
|
123
|
-
|
124
|
-
private lateinit var appBarConfiguration: AppBarConfiguration
|
125
|
-
private lateinit var binding: ActivityMainBinding
|
126
|
-
private lateinit var realm:Realm
|
127
|
-
|
128
|
-
private lateinit var adapter:CustomRecyclerViewAdapter
|
129
|
-
private lateinit var layoutManager:RecyclerView.LayoutManager
|
130
|
-
|
131
|
-
override fun onCreate(savedInstanceState: Bundle?) {
|
132
|
-
super.onCreate(savedInstanceState)
|
133
|
-
|
134
|
-
binding = ActivityMainBinding.inflate(layoutInflater)
|
135
|
-
setContentView(binding.root)
|
136
|
-
setSupportActionBar(binding.toolbar)
|
137
|
-
realm = Realm.getDefaultInstance()
|
138
|
-
|
139
|
-
binding.fab.setOnClickListener { view ->
|
140
|
-
val intent = Intent(this,EditActivity::class.java)
|
141
|
-
startActivity(intent)
|
142
|
-
}
|
143
|
-
}
|
144
|
-
|
145
|
-
override fun onStart() {
|
146
|
-
super.onStart()
|
147
|
-
val recyclerView:RecyclerView = findViewById(R.id.recyclerView)
|
148
|
-
val realmResults = realm.where(BoodPress::class.java)
|
149
|
-
.findAll()
|
150
|
-
.sort("id",Sort.DESCENDING)
|
151
|
-
layoutManager = LinearLayoutManager(this)
|
152
|
-
recyclerView.layoutManager = layoutManager
|
153
|
-
|
154
|
-
adapter = CustomRecyclerViewAdapter(realmResults)
|
155
|
-
recyclerView.adapter = this.adapter
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
override fun onDestroy() {
|
160
|
-
super.onDestroy()
|
161
|
-
realm.close()
|
162
|
-
}
|
163
|
-
|
164
|
-
}
|
165
|
-
```
|
166
17
|
```EditActivity.kt
|
167
18
|
package com.example.samplerealm
|
168
19
|
|
@@ -178,18 +29,17 @@
|
|
178
29
|
|
179
30
|
class EditActivity : AppCompatActivity() {
|
180
31
|
private val tag = "BloodPressure"
|
181
|
-
private lateinit var realm:
|
32
|
+
private lateinit var realm:Realm
|
182
33
|
|
183
34
|
override fun onCreate(savedInstanceState: Bundle?) {
|
184
35
|
super.onCreate(savedInstanceState)
|
185
36
|
setContentView(R.layout.activity_edit)
|
186
|
-
|
187
37
|
val maxEdit:EditText= findViewById(R.id.maxEdit)
|
188
38
|
val minEdit:EditText= findViewById(R.id.minEdit)
|
189
|
-
val pulseEdit:EditText =findViewById(R.id.pulseEdit)
|
39
|
+
val pulseEdit: EditText =findViewById(R.id.pulseEdit)
|
190
40
|
val btnSave:Button = findViewById(R.id.btnSave)
|
191
|
-
val btnDelete:Button=findViewById(R.id.btnDelete)
|
41
|
+
val btnDelete: Button =findViewById(R.id.btnDelete)
|
192
|
-
realm= Realm.getDefaultInstance()
|
42
|
+
realm= Realm.getDefaultInstance()//(※クラッシュ)
|
193
43
|
|
194
44
|
btnSave.setOnClickListener {
|
195
45
|
var max:Long =0
|
@@ -199,82 +49,42 @@
|
|
199
49
|
if(!maxEdit.text.isNullOrEmpty()){
|
200
50
|
max = maxEdit.text.toString().toLong()
|
201
51
|
}
|
202
|
-
|
203
52
|
if(!minEdit.text.isNullOrEmpty()){
|
204
53
|
min = minEdit.text.toString().toLong()
|
205
54
|
}
|
206
|
-
|
207
55
|
if(pulseEdit.text.isNullOrEmpty()){
|
208
56
|
pulse=pulseEdit.text.toString().toLong()
|
209
57
|
}
|
210
58
|
|
211
|
-
realm.executeTransaction
|
59
|
+
realm.executeTransaction{
|
212
|
-
val maxId = realm.where<BoodPress>().max("id")
|
60
|
+
val maxId = realm.where<BloodPress>().max("id")
|
213
61
|
val nextId =(maxId?.toLong()?:0L)+1L
|
214
|
-
val bloodPress =realm.createObject<BoodPress>(nextId)
|
62
|
+
val bloodPress =realm.createObject<BloodPress>(nextId)
|
215
|
-
|
216
63
|
bloodPress.dateTime = Date()
|
217
64
|
bloodPress.max = max
|
218
65
|
bloodPress.mim = min
|
219
66
|
bloodPress.pulse = pulse
|
220
67
|
}
|
221
|
-
|
222
68
|
Toast.makeText(applicationContext,"保存しました",Toast.LENGTH_LONG).show()
|
223
69
|
finish()
|
224
70
|
}
|
225
|
-
|
226
71
|
}
|
227
|
-
|
228
72
|
override fun onDestroy() {
|
229
73
|
super.onDestroy()
|
230
74
|
realm.close()
|
231
75
|
}
|
232
76
|
}
|
233
77
|
```
|
234
|
-
```CustomRecyclerViewAdapter.kt.kt
|
235
|
-
package com.example.samplerealm
|
236
78
|
|
237
|
-
import android.graphics.Color
|
238
|
-
import android.view.LayoutInflater
|
239
|
-
import android.text.format.DateFormat
|
240
|
-
import android.view.ViewGroup
|
241
|
-
import androidx.recyclerview.widget.RecyclerView
|
242
|
-
import io.realm.RealmResults
|
243
|
-
|
244
|
-
class CustomRecyclerViewAdapter(realmResults:RealmResults<BoodPress>):RecyclerView.Adapter<ViewHolder>() {
|
245
|
-
private val rResults:RealmResults<BoodPress> = realmResults
|
246
|
-
|
247
|
-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
248
|
-
//TODO("Not yet implemented")
|
249
|
-
val view = LayoutInflater.from(parent.context)
|
250
|
-
.inflate(R.layout.one_result,parent,false)
|
251
|
-
val viewholder =ViewHolder(view)
|
252
|
-
return viewholder
|
253
|
-
}
|
254
|
-
|
255
|
-
override fun getItemCount(): Int {
|
256
|
-
//TODO("Not yet implemented")
|
257
|
-
return rResults.size
|
258
|
-
}
|
259
|
-
|
260
|
-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
261
|
-
//TODO("Not yet implemented")
|
262
|
-
val bloodPress = rResults[position]
|
263
|
-
holder.dateText?.text = DateFormat.format("yyyy/MM/dd kk:mm",bloodPress?.dateTime)
|
264
|
-
holder.minMaxText?.text="${bloodPress?.max.toString()}/${bloodPress?.mim.toString()}"
|
265
|
-
holder.pulseText?.text=bloodPress?.pulse.toString()
|
266
|
-
holder.itemView.setBackgroundColor(if(position % 2 == 0) Color.LTGRAY else Color.WHITE)
|
267
|
-
}
|
268
|
-
}
|
269
|
-
```
|
270
79
|
|
271
80
|
### 試したこと
|
272
81
|
|
273
|
-
|
82
|
+
realm= Realm.getDefaultInstance()
|
274
|
-
|
83
|
+
の一文を消すと、エミュレータは正常に起動する。
|
84
|
+
ただし、realm= Realm.getDefaultInstance()を消していいのか、
|
275
|
-
|
85
|
+
あるいは別の書き方にしないといけないのか、判断できないため質問させていただきました。
|
276
86
|
|
277
87
|
### 補足情報(FW/ツールのバージョンなど)
|
278
88
|
|
279
|
-
AndroidStudio
|
89
|
+
AndroidStudio Chipmunk | 2021.2.1
|
280
90
|
|
3
タイトル修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Realm】保存ボタンを押しても、画面に反映されません。どこが間違っているのでしょうか?
|
1
|
+
【Realm】保存ボタンを押しても、画面に反映されません。どこが間違っているのでしょうか?(そもそもRelmのbuid_gradle追記文はこれで正しいのでしょうか?)
|
test
CHANGED
File without changes
|
2
補足追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
|
9
9
|
保存ボタン(btnSave)が押せるところまで出来たが、その内容が画面に反映されない(真っ白のまま)なので
|
10
10
|
どこの記述が間違っているか指摘・添削いただきたい。
|
11
|
+
(※特にbuid_gradleの2つは、そもそもネットで追記するコードがバラバラなので、個人的にはここが一番怪しいと思っています。)
|
11
12
|
|
12
13
|
### 発生している問題・エラーメッセージ
|
13
14
|
|
1
コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,6 +16,76 @@
|
|
16
16
|
```
|
17
17
|
|
18
18
|
### 該当のソースコード
|
19
|
+
```buid_gradle(project)
|
20
|
+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
21
|
+
buildscript {
|
22
|
+
dependencies {
|
23
|
+
classpath "io.realm:realm-gradle-plugin:10.8.0"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
plugins {
|
28
|
+
id 'com.android.application' version '7.1.3' apply false
|
29
|
+
id 'com.android.library' version '7.1.3' apply false
|
30
|
+
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
|
31
|
+
}
|
32
|
+
|
33
|
+
task clean(type: Delete) {
|
34
|
+
delete rootProject.buildDir
|
35
|
+
}
|
36
|
+
```
|
37
|
+
```buid_gradle(app)
|
38
|
+
plugins {
|
39
|
+
id 'com.android.application'
|
40
|
+
id 'org.jetbrains.kotlin.android'
|
41
|
+
id 'kotlin-kapt'
|
42
|
+
id 'realm-android'
|
43
|
+
}
|
44
|
+
|
45
|
+
android {
|
46
|
+
compileSdk 31
|
47
|
+
|
48
|
+
defaultConfig {
|
49
|
+
applicationId "com.example.samplerealm"
|
50
|
+
minSdk 21
|
51
|
+
targetSdk 31
|
52
|
+
versionCode 1
|
53
|
+
versionName "1.0"
|
54
|
+
|
55
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
56
|
+
}
|
57
|
+
|
58
|
+
buildTypes {
|
59
|
+
release {
|
60
|
+
minifyEnabled false
|
61
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
62
|
+
}
|
63
|
+
}
|
64
|
+
compileOptions {
|
65
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
66
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
67
|
+
}
|
68
|
+
kotlinOptions {
|
69
|
+
jvmTarget = '1.8'
|
70
|
+
}
|
71
|
+
buildFeatures {
|
72
|
+
viewBinding true
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
dependencies {
|
77
|
+
|
78
|
+
implementation 'androidx.core:core-ktx:1.7.0'
|
79
|
+
implementation 'androidx.appcompat:appcompat:1.4.1'
|
80
|
+
implementation 'com.google.android.material:material:1.5.0'
|
81
|
+
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
82
|
+
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
|
83
|
+
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
|
84
|
+
testImplementation 'junit:junit:4.13.2'
|
85
|
+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
86
|
+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
87
|
+
}
|
88
|
+
```
|
19
89
|
|
20
90
|
```content_main.xml
|
21
91
|
<?xml version="1.0" encoding="utf-8"?>
|