質問編集履歴

1

抜粋コードを削除し、全体コードを載せました

2018/07/25 14:58

投稿

Komugi_Haiga
Komugi_Haiga

スコア12

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,10 @@
30
30
 
31
31
 
32
32
 
33
+ package com.komugi.haiga.samplesqlite
34
+
35
+
36
+
33
37
  import android.support.v7.app.AppCompatActivity
34
38
 
35
39
  import android.os.Bundle
@@ -62,15 +66,55 @@
62
66
 
63
67
  val listView = findViewById<ListView>(R.id.listView)
64
68
 
65
- listView.adapter = ArrayAdapter<String>(this, R.layout.list_text_row, R.id.textView, texts)
69
+ listView.adapter = ArrayAdapter<MutableList<String>>(this, R.layout.list_text_row, R.id.textView, texts)
66
-
70
+
67
- }
71
+ }
72
+
73
+
74
+
75
+
76
+
68
-
77
+ val button = findViewById<Button>(R.id.button)
78
+
69
-
79
+ button.setOnClickListener {
80
+
70
-
81
+ val editText = findViewById<EditText>(R.id.editText)
82
+
83
+ insertText(this, editText.text.toString())
84
+
85
+ show()
86
+
87
+ }
88
+
89
+
90
+
91
+ val buttonDelete = findViewById<Button>(R.id.button_delete)
92
+
93
+ buttonDelete.setOnClickListener {
94
+
95
+ //全削除する前に確認する
96
+
97
+ AlertDialog.Builder(this)
98
+
99
+ .setTitle("全削除")
100
+
101
+ .setMessage("全削除します")
102
+
103
+ .setNegativeButton("Cancel", null)
104
+
105
+ .setPositiveButton("OK") { _, _ ->
106
+
107
+ //データベースを全削除する
108
+
109
+ deleteText(this)
110
+
111
+ show()
112
+
71
- ...
113
+ }.show()
114
+
72
-
115
+ }
73
-
116
+
117
+
74
118
 
75
119
  val adapter = ArrayAdapter<String>(this, R.layout.list_text_row, R.id.textView)
76
120
 
@@ -78,7 +122,7 @@
78
122
 
79
123
  selectedList.adapter = adapter
80
124
 
81
- selectedList.setOnItemClickListener { _, _, _, _ ->
125
+ selectedList.setOnItemClickListener { _, _, position, _ ->
82
126
 
83
127
  AlertDialog.Builder(this)
84
128
 
@@ -90,7 +134,7 @@
90
134
 
91
135
  .setPositiveButton("OK") {_, _ ->
92
136
 
93
- deleteItem(this)
137
+ deleteItem(this, ids[position])
94
138
 
95
139
  show()
96
140
 
@@ -106,12 +150,6 @@
106
150
 
107
151
 
108
152
 
109
-
110
-
111
-
112
-
113
-
114
-
115
153
  ```
116
154
 
117
155
  ```
@@ -120,6 +158,10 @@
120
158
 
121
159
 
122
160
 
161
+ package com.komugi.haiga.samplesqlite
162
+
163
+
164
+
123
165
  import android.content.ContentValues
124
166
 
125
167
  import android.content.Context
@@ -128,12 +170,6 @@
128
170
 
129
171
  import android.database.sqlite.SQLiteOpenHelper
130
172
 
131
- import java.nio.file.Files.delete
132
-
133
-
134
-
135
-
136
-
137
173
 
138
174
 
139
175
 
@@ -144,19 +180,105 @@
144
180
 
145
181
 
146
182
 
183
+ fun queryTexts(context: Context) : Array<MutableList<String>> {
184
+
185
+ //読み込み用のデータベースを開く
186
+
187
+ val database = SampleDatabaseOpenHelper(context).readableDatabase
188
+
189
+ //データベースから全件検索する
190
+
191
+ val cursor = database.query("texts", null, null, null, null, null,"created_at DESC")
192
+
193
+
194
+
195
+ val texts = mutableListOf<String>()
196
+
197
+ val ids = mutableListOf<String>()
198
+
199
+ cursor.use {
200
+
201
+ //カーソルで順次処理していく
202
+
203
+ while(cursor.moveToNext()) {
204
+
205
+ //保存されているテキストを得る
206
+
207
+ val text = cursor.getString(cursor.getColumnIndex("text"))
208
+
209
+ val id = cursor.getString(cursor.getColumnIndex("id"))
210
+
211
+ texts.add(text)
212
+
147
- ...
213
+ ids.add(id)
214
+
215
+ }
216
+
217
+ }
218
+
219
+ val resArray = arrayOf(texts)
220
+
221
+ database.close()
222
+
223
+ return resArray
224
+
225
+ }
226
+
227
+
228
+
229
+ fun insertText(context: Context, text: String) {
230
+
231
+ //書き込み可能なデータベースを開く
232
+
233
+ val database = SampleDatabaseOpenHelper(context).writableDatabase
234
+
235
+
236
+
237
+ database.use { db ->
238
+
239
+ //挿入するレコード
240
+
241
+ val record = ContentValues().apply {
242
+
243
+ put("text", text)
244
+
245
+ }
246
+
247
+ //データベースに挿入する
248
+
249
+ db.insert("texts", null, record)
250
+
251
+ }
252
+
253
+ }
254
+
255
+
256
+
257
+ //データベース全削除機能
258
+
259
+ fun deleteText(context: Context) {
260
+
261
+ val database = SampleDatabaseOpenHelper(context).writableDatabase
262
+
263
+ database.use { db ->
264
+
265
+ db.delete("texts", null, null)
266
+
267
+ }
268
+
269
+ }
148
270
 
149
271
 
150
272
 
151
273
  //データベース選択削除機能
152
274
 
153
- fun deleteItem(context: Context) {
275
+ fun deleteItem(context: Context, id: String) {
154
276
 
155
277
  val database = SampleDatabaseOpenHelper(context).writableDatabase
156
278
 
157
279
  database.use { db ->
158
280
 
159
- db.delete("texts", "id = ?", null)
281
+ db.delete("texts", "id =" + id, null)
160
282
 
161
283
  }
162
284
 
@@ -176,7 +298,7 @@
176
298
 
177
299
  db?.execSQL("CREATE TABLE texts ( " +
178
300
 
179
- " id = INTEGER PRIMARY KEY AUTOINCREMENT, "+
301
+ " id INTEGER PRIMARY KEY AUTOINCREMENT, "+
180
302
 
181
303
  " text TEXT NOT NULL, " +
182
304
 
@@ -196,4 +318,6 @@
196
318
 
197
319
 
198
320
 
321
+
322
+
199
323
  ```