質問するログイン新規登録

質問編集履歴

3

説明文補足

2020/07/08 09:00

投稿

tege
tege

スコア29

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,15 @@
1
1
  Realmでflagカラムを追加を行ったのですが、下記のエラーが出てしまいます。
2
- 何か解決策はないでしょう
2
+ deleteIfMigrationNeeded()とうメソッド一度realmを削除てくれる事がわったのですが、どこに記載すればいいのかわかりません。どなたか教えていただきたいです。
3
+
3
4
  ```
5
+ E/AndroidRuntime: FATAL EXCEPTION: main
6
+ Process: com.example.myscheduler, PID: 32178
7
+ java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myscheduler/com.example.myscheduler.MainActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
8
+ - Property 'Schedule.flag' has been added.
9
+ ```
4
10
 
11
+ ```
12
+
5
13
  import io.realm.RealmObject
6
14
  import io.realm.annotations.PrimaryKey
7
15
  import java.util.*
@@ -16,13 +24,6 @@
16
24
  }
17
25
  ```
18
26
 
19
- ```
20
- E/AndroidRuntime: FATAL EXCEPTION: main
21
- Process: com.example.myscheduler, PID: 32178
22
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myscheduler/com.example.myscheduler.MainActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
23
- - Property 'Schedule.flag' has been added.
24
- ```
25
-
26
27
  ```kotlin
27
28
  //////////RecyclerViewにレイアウトマネージャーとアダプタを設定する/////////////
28
29
  class MainActivity : AppCompatActivity() {
@@ -55,19 +56,4 @@
55
56
  }
56
57
  }
57
58
 
58
- ```
59
-
60
- ``` kotlin
61
- package com.example.myscheduler
62
-
63
- import android.app.Application
64
- import io.realm.Realm
65
-
66
- class MySchedulerApplication : Application() {
67
- override fun onCreate() {
68
- super.onCreate()
69
- Realm.init(this)
70
- }
71
- }
72
- コード
73
59
  ```

2

質問の変更

2020/07/08 09:00

投稿

tege
tege

スコア29

title CHANGED
@@ -1,1 +1,1 @@
1
- 完了ボタンが押された時該当するIDのデータの色を変
1
+ Realmでカラムを変更し際にエラーがでます
body CHANGED
@@ -1,68 +1,73 @@
1
- 完了ボタンが押された場合現在表示されているデータに該当するIDのデータの色変更しのですが、Realmにflagを用意して0完了前。1が完了とったようにるべきでしょうか?
1
+ Realmでflagカラム追加を行ったのですが、下記のエラー出てしま
2
+ 何か解決策はないでしょうか?
3
+ ```
2
4
 
3
- ```表示画面
4
- import android.text.format.DateFormat
5
- import android.view.LayoutInflater
6
- import android.view.View
7
- import android.view.ViewGroup
8
- import android.widget.TextView
9
- import androidx.recyclerview.widget.RecyclerView
10
- import io.realm.OrderedRealmCollection
5
+ import io.realm.RealmObject
11
- import io.realm.RealmRecyclerViewAdapter
6
+ import io.realm.annotations.PrimaryKey
7
+ import java.util.*
12
8
 
13
- class ScheduleAdapter(data: OrderedRealmCollection<Schedule>) :
9
+ open class Schedule : RealmObject() {
10
+ @PrimaryKey
11
+ var id: Long = 0
12
+ var date: Date = Date()
13
+ var title: String = ""
14
- RealmRecyclerViewAdapter<Schedule, ScheduleAdapter.ViewHolder>(data, true) {
14
+ var detail: String = ""
15
+ var flag: Long = 0
16
+ }
17
+ ```
15
18
 
16
- private var listener: ((Long?) -> Unit)? = null
17
- fun setOnItemClickListener(listener: (Long?) -> Unit) {
18
- this.listener = listener
19
- }
19
+ ```
20
+ E/AndroidRuntime: FATAL EXCEPTION: main
21
+ Process: com.example.myscheduler, PID: 32178
22
+ java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myscheduler/com.example.myscheduler.MainActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
23
+ - Property 'Schedule.flag' has been added.
24
+ ```
20
25
 
21
- init {
26
+ ```kotlin
27
+ //////////RecyclerViewにレイアウトマネージャーとアダプタを設定する/////////////
22
- setHasStableIds(true)
28
+ class MainActivity : AppCompatActivity() {
23
- }
29
+ private lateinit var realm: Realm //Realmクラスのプロパティを用意
24
30
 
31
+ override fun onCreate(savedInstanceState: Bundle?) {
32
+ super.onCreate(savedInstanceState)
33
+ setContentView(R.layout.activity_main)
34
+ setSupportActionBar(toolbar)
25
- class ViewHolder(cell: View) : RecyclerView.ViewHolder(cell) { //RecylerView継承
35
+ realm = Realm.getDefaultInstance() //Realmクラスのインスタンス取得
36
+ list.layoutManager = LinearLayoutManager(this)
37
+ val schedules = realm.where<Schedule>().sort("date",Sort.DESCENDING).findAll() //findAllで全てのスケジュールを取得。変数に格納
26
- val date: TextView = cell.findViewById(android.R.id.text1)
38
+ val adapter = ScheduleAdapter(schedules)
27
- val title: TextView = cell.findViewById(android.R.id.text2)
39
+ list.adapter = adapter
28
- }
29
40
 
30
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScheduleAdapter.ViewHolder {
31
- val infrater = LayoutInflater.from(parent.context) //インスタンス生成
32
- val view = infrater.inflate( //ビューにレイアウトXMLを適用
33
- android.R.layout.simple_list_item_2,
34
- parent, false
35
- )
36
- return ViewHolder(view)
37
- }
38
-
39
- ////////////////////////// //データを取り出し表示させるための処理/////////////
40
- override fun onBindViewHolder(holder: ScheduleAdapter.ViewHolder, position: Int) {
41
- val schedule: Schedule? = getItem(position)
42
- holder.date.text = DateFormat.format("yyyy/MM/dd", schedule?.date)
43
- holder.title.text = schedule?.title
44
- holder.itemView.setOnClickListener {
41
+ fab.setOnClickListener { view ->
42
+ val intent = Intent(this, ScheduleEditActivity::class.java)
45
- listener?.invoke(schedule?.id)
43
+ startActivity(intent)
46
44
  }
45
+ adapter.setOnItemClickListener { id ->
46
+ val intent = Intent(this, ScheduleEditActivity::class.java)
47
+ .putExtra("schedule_id", id)
48
+ startActivity(intent)
49
+ }
47
50
  }
48
51
 
49
- override fun getItemId(position: Int): Long { //getItemIdをオーバーライド
50
- return getItem(position)?.id ?: 0
52
+ override fun onDestroy() {
53
+ super.onDestroy()
54
+ realm.close()
51
55
  }
52
56
  }
57
+
53
58
  ```
54
59
 
55
- ```
60
+ ``` kotlin
56
- /////////////完了ボタンが押された場合///////////////////////////
57
- comp.setOnClickListener { view: View ->
58
- realm.executeTransaction { db: Realm ->
59
- db.where<Schedule>().equalTo("id", scheduleId)
60
- }
61
- ///////////////////////////スナックバー//////////////////////////
62
- Snackbar.make(view, "完了しました", Snackbar.LENGTH_INDEFINITE)
63
- .setAction("戻る") { finish() }
64
- .setActionTextColor(Color.YELLOW)
61
+ package com.example.myscheduler
65
- .show()
66
- }
67
62
 
63
+ import android.app.Application
64
+ import io.realm.Realm
65
+
66
+ class MySchedulerApplication : Application() {
67
+ override fun onCreate() {
68
+ super.onCreate()
69
+ Realm.init(this)
70
+ }
71
+ }
72
+ コード
68
73
  ```

1

誤字の修正

2020/07/08 07:22

投稿

tege
tege

スコア29

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- 完了ボタンが押された場合現在表示されているデータに該当するIDのデータの色を変更したいのですが、Realmにflagを用意して0が完了前。1が完了といったようにするやるべきでしょうか?
1
+ 完了ボタンが押された場合現在表示されているデータに該当するIDのデータの色を変更したいのですが、Realmにflagを用意して0が完了前。1が完了といったようにするべきでしょうか?
2
2
 
3
3
  ```表示画面
4
4
  import android.text.format.DateFormat