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

質問編集履歴

1

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

2018/07/25 14:58

投稿

Komugi_Haiga
Komugi_Haiga

スコア12

title CHANGED
File without changes
body CHANGED
@@ -14,6 +14,8 @@
14
14
  ```
15
15
  (MainActivity.kt)
16
16
 
17
+ package com.komugi.haiga.samplesqlite
18
+
17
19
  import android.support.v7.app.AppCompatActivity
18
20
  import android.os.Bundle
19
21
  import android.support.v7.app.AlertDialog
@@ -30,21 +32,41 @@
30
32
  //データベースに登録されている文字列の一覧を得る
31
33
  val texts = queryTexts(this)
32
34
  val listView = findViewById<ListView>(R.id.listView)
33
- listView.adapter = ArrayAdapter<String>(this, R.layout.list_text_row, R.id.textView, texts)
35
+ listView.adapter = ArrayAdapter<MutableList<String>>(this, R.layout.list_text_row, R.id.textView, texts)
34
36
  }
35
37
 
38
+
39
+ val button = findViewById<Button>(R.id.button)
40
+ button.setOnClickListener {
41
+ val editText = findViewById<EditText>(R.id.editText)
42
+ insertText(this, editText.text.toString())
43
+ show()
44
+ }
45
+
46
+ val buttonDelete = findViewById<Button>(R.id.button_delete)
47
+ buttonDelete.setOnClickListener {
48
+ //全削除する前に確認する
49
+ AlertDialog.Builder(this)
50
+ .setTitle("全削除")
51
+ .setMessage("全削除します")
52
+ .setNegativeButton("Cancel", null)
53
+ .setPositiveButton("OK") { _, _ ->
54
+ //データベースを全削除する
55
+ deleteText(this)
56
+ show()
36
- ...
57
+ }.show()
37
-
58
+ }
59
+
38
60
  val adapter = ArrayAdapter<String>(this, R.layout.list_text_row, R.id.textView)
39
61
  val selectedList = findViewById<ListView>(R.id.listView)
40
62
  selectedList.adapter = adapter
41
- selectedList.setOnItemClickListener { _, _, _, _ ->
63
+ selectedList.setOnItemClickListener { _, _, position, _ ->
42
64
  AlertDialog.Builder(this)
43
65
  .setTitle("削除")
44
66
  .setMessage("削除します")
45
67
  .setNegativeButton("Cancel", null)
46
68
  .setPositiveButton("OK") {_, _ ->
47
- deleteItem(this)
69
+ deleteItem(this, ids[position])
48
70
  show()
49
71
  }.show()
50
72
  }
@@ -52,32 +74,71 @@
52
74
  }
53
75
 
54
76
 
55
-
56
-
57
-
58
77
  ```
59
78
  ```
60
79
  (SampleDatabase.kt)
61
80
 
81
+ package com.komugi.haiga.samplesqlite
82
+
62
83
  import android.content.ContentValues
63
84
  import android.content.Context
64
85
  import android.database.sqlite.SQLiteDatabase
65
86
  import android.database.sqlite.SQLiteOpenHelper
66
- import java.nio.file.Files.delete
67
87
 
68
88
 
69
-
70
-
71
89
  private const val DB_NAME = "SampleDatabase"
72
90
  private const val DB_VERSION = 1
73
91
 
92
+ fun queryTexts(context: Context) : Array<MutableList<String>> {
93
+ //読み込み用のデータベースを開く
94
+ val database = SampleDatabaseOpenHelper(context).readableDatabase
74
- ...
95
+ //データベースから全件検索する
96
+ val cursor = database.query("texts", null, null, null, null, null,"created_at DESC")
75
97
 
98
+ val texts = mutableListOf<String>()
99
+ val ids = mutableListOf<String>()
100
+ cursor.use {
101
+ //カーソルで順次処理していく
102
+ while(cursor.moveToNext()) {
103
+ //保存されているテキストを得る
104
+ val text = cursor.getString(cursor.getColumnIndex("text"))
105
+ val id = cursor.getString(cursor.getColumnIndex("id"))
106
+ texts.add(text)
107
+ ids.add(id)
108
+ }
109
+ }
110
+ val resArray = arrayOf(texts)
111
+ database.close()
112
+ return resArray
113
+ }
114
+
115
+ fun insertText(context: Context, text: String) {
116
+ //書き込み可能なデータベースを開く
117
+ val database = SampleDatabaseOpenHelper(context).writableDatabase
118
+
119
+ database.use { db ->
120
+ //挿入するレコード
121
+ val record = ContentValues().apply {
122
+ put("text", text)
123
+ }
124
+ //データベースに挿入する
125
+ db.insert("texts", null, record)
126
+ }
127
+ }
128
+
129
+ //データベース全削除機能
130
+ fun deleteText(context: Context) {
131
+ val database = SampleDatabaseOpenHelper(context).writableDatabase
132
+ database.use { db ->
133
+ db.delete("texts", null, null)
134
+ }
135
+ }
136
+
76
137
  //データベース選択削除機能
77
- fun deleteItem(context: Context) {
138
+ fun deleteItem(context: Context, id: String) {
78
139
  val database = SampleDatabaseOpenHelper(context).writableDatabase
79
140
  database.use { db ->
80
- db.delete("texts", "id = ?", null)
141
+ db.delete("texts", "id =" + id, null)
81
142
  }
82
143
  }
83
144
 
@@ -87,7 +148,7 @@
87
148
  override fun onCreate(db: SQLiteDatabase?) {
88
149
  //テーブルの作成
89
150
  db?.execSQL("CREATE TABLE texts ( " +
90
- " id = INTEGER PRIMARY KEY AUTOINCREMENT, "+
151
+ " id INTEGER PRIMARY KEY AUTOINCREMENT, "+
91
152
  " text TEXT NOT NULL, " +
92
153
  " created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ")
93
154
  }
@@ -97,4 +158,5 @@
97
158
  }
98
159
  }
99
160
 
161
+
100
162
  ```