質問編集履歴
1
コードを変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,51 +7,137 @@
|
|
7
7
|
|
8
8
|
### 該当のソースコード
|
9
9
|
|
10
|
-
```
|
10
|
+
```MainActivity
|
11
|
+
val et_name : EditText = findViewById(R.id.et_name)
|
11
|
-
|
12
|
+
val bt_add : Button = findViewById(R.id.bt_add)
|
12
|
-
|
13
|
+
val bt_delete : Button = findViewById(R.id.bt_delete)
|
13
14
|
val lv : ListView = findViewById(R.id.lv)
|
14
|
-
val bt_add : Button = findViewById(R.id.bt_add)
|
15
15
|
|
16
|
-
|
17
|
-
//アダプターに入れてListViewにセットする
|
18
|
-
val adapter = ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,
|
16
|
+
val adapter = ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,
|
17
|
+
mutableListOf())
|
19
18
|
lv.adapter = adapter
|
20
19
|
|
21
|
-
//
|
20
|
+
//Realmを使う時に必要
|
21
|
+
val realm = Realm.getDefaultInstance()
|
22
|
+
|
23
|
+
//bt_addを押すとet_nameに入力した内容をDBに保存とListViewに表示する処理を記載する
|
22
24
|
bt_add.setOnClickListener {
|
23
|
-
|
24
|
-
|
25
|
+
if ( et_name.text.toString().isEmpty()){
|
25
|
-
|
26
|
-
|
26
|
+
AlertDialog.Builder(this)
|
27
|
-
|
27
|
+
.setTitle("未入力")
|
28
|
-
|
28
|
+
.setMessage("名前を入力して下さい")
|
29
|
-
.setView(et)
|
30
|
-
.setPositiveButton("追加",DialogInterface.OnClickListener { dialog, which ->
|
31
|
-
//adapterに追加する
|
32
|
-
val myTodo = et.text.toString()
|
33
|
-
adapter.add(myTodo)
|
34
|
-
})
|
35
|
-
|
29
|
+
.setPositiveButton("OK",null)
|
30
|
+
.show()//et_nameが未入力の場合、アラートダイアログで表示
|
36
|
-
|
31
|
+
}else{
|
37
|
-
|
32
|
+
realm.beginTransaction()//Realmのはじまり
|
33
|
+
try {
|
34
|
+
val user = realm.createObject(user::class.java)
|
35
|
+
user.setName(et_name.text.toString())//DBに書き込み
|
36
|
+
et_name.text.clear()//DBに書き込みしたらEditTextの文字をクリアする
|
37
|
+
Toast.makeText(applicationContext,"登録しました",Toast.LENGTH_SHORT).show()//トーストで表示
|
38
|
+
realm.commitTransaction()
|
39
|
+
adapter.add(user.getName())//et_nameの内容をListViewに追加する
|
40
|
+
}catch (e:RealmException){}
|
41
|
+
}
|
38
42
|
}
|
39
43
|
|
40
|
-
//ListViewをタッ
|
44
|
+
//ListViewをタップするとアラートダイアログを表示して、削除するかしないか判断する
|
45
|
+
//合わせてDBに登録されているデータも一緒に削除する
|
41
46
|
lv.setOnItemClickListener { parent, view, position, id ->
|
42
47
|
AlertDialog.Builder(this)
|
43
|
-
.setTitle("削除しますか?")
|
48
|
+
.setTitle("削除しますか??")
|
49
|
+
//Yesを押下した時の処理
|
50
|
+
//タップされたところの名前を削除とDBの削除
|
44
|
-
.setPositiveButton("
|
51
|
+
.setPositiveButton("Yes",DialogInterface.OnClickListener { dialog, which ->
|
45
|
-
adapter.remove(adapter.getItem(id.toInt()))
|
52
|
+
adapter.remove(adapter.getItem(id.toInt()))
|
46
|
-
adapter.notifyDataSetChanged()
|
53
|
+
adapter.notifyDataSetChanged()
|
54
|
+
//ここからDBの削除の処理を記載する
|
55
|
+
//現時点でタップされたとこの名前のDB削除方法は分からない
|
56
|
+
realm.beginTransaction()
|
57
|
+
val delete = adapter.getItem(id.toInt())
|
58
|
+
realm.where(user::class.java)
|
59
|
+
et_name.text.clear()
|
60
|
+
//Toast.makeText(applicationContext,"削除しました",Toast.LENGTH_SHORT).show()
|
61
|
+
realm.commitTransaction()
|
47
62
|
})
|
48
|
-
.setNegativeButton("
|
63
|
+
.setNegativeButton("No",DialogInterface.OnClickListener { dialog, which ->
|
64
|
+
Toast.makeText(applicationContext,"キャンセルしました",Toast.LENGTH_SHORT).show()
|
65
|
+
})
|
49
66
|
.show()
|
50
67
|
}
|
68
|
+
//これは予備コード
|
69
|
+
//DB全削除用
|
70
|
+
bt_delete.setOnClickListener {
|
71
|
+
realm.beginTransaction()
|
72
|
+
realm.where(user::class.java).findAll().deleteAllFromRealm()
|
73
|
+
realm.commitTransaction()
|
74
|
+
}
|
51
75
|
}
|
52
76
|
}
|
53
77
|
```
|
54
78
|
|
79
|
+
```user
|
80
|
+
open class user : RealmObject(){
|
81
|
+
private var name : String = ""
|
82
|
+
|
83
|
+
fun setName(name:String){
|
84
|
+
this.name = name
|
85
|
+
}
|
86
|
+
|
87
|
+
fun getName():String{
|
88
|
+
return name
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
```
|
93
|
+
```activitymainXML
|
94
|
+
|
95
|
+
<?xml version="1.0" encoding="utf-8"?>
|
96
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
97
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
98
|
+
xmlns:tools="http://schemas.android.com/tools"
|
99
|
+
android:layout_width="match_parent"
|
100
|
+
android:layout_height="match_parent"
|
101
|
+
tools:context=".MainActivity">
|
102
|
+
|
103
|
+
<EditText
|
104
|
+
android:id="@+id/et_name"
|
105
|
+
android:layout_width="match_parent"
|
106
|
+
android:layout_height="wrap_content"
|
107
|
+
android:ems="10"
|
108
|
+
android:inputType="textPersonName"
|
109
|
+
android:hint="name"
|
110
|
+
app:layout_constraintStart_toStartOf="parent"
|
111
|
+
app:layout_constraintTop_toTopOf="parent" />
|
112
|
+
|
113
|
+
<Button
|
114
|
+
android:id="@+id/bt_add"
|
115
|
+
android:layout_width="wrap_content"
|
116
|
+
android:layout_height="wrap_content"
|
117
|
+
android:text="add"
|
118
|
+
app:layout_constraintEnd_toEndOf="parent"
|
119
|
+
app:layout_constraintTop_toBottomOf="@+id/et_name" />
|
120
|
+
|
121
|
+
<ListView
|
122
|
+
android:id="@+id/lv"
|
123
|
+
android:layout_width="409dp"
|
124
|
+
android:layout_height="636dp"
|
125
|
+
android:layout_marginStart="1dp"
|
126
|
+
android:layout_marginTop="1dp"
|
127
|
+
android:layout_marginEnd="1dp"
|
128
|
+
app:layout_constraintEnd_toEndOf="parent"
|
129
|
+
app:layout_constraintStart_toStartOf="parent"
|
130
|
+
app:layout_constraintTop_toBottomOf="@+id/bt_add" />
|
131
|
+
|
132
|
+
<Button
|
133
|
+
android:id="@+id/bt_delete"
|
134
|
+
android:layout_width="wrap_content"
|
135
|
+
android:layout_height="wrap_content"
|
136
|
+
android:text="Button"
|
137
|
+
app:layout_constraintEnd_toStartOf="@+id/bt_add"
|
138
|
+
app:layout_constraintTop_toBottomOf="@+id/et_name" />
|
139
|
+
```
|
140
|
+
|
55
141
|
### 補足情報(FW/ツールのバージョンなど)
|
56
142
|
|
57
143
|
Android Studio Arctic Fox | 2020.3.1 Patch 1
|