回答編集履歴

3

コメントに対する回答2

2018/07/26 09:40

投稿

razuma
razuma

スコア1313

test CHANGED
@@ -235,3 +235,25 @@
235
235
 
236
236
 
237
237
  無事に動くと良いですが・・・。
238
+
239
+
240
+
241
+ さらにさらにコメントに回答を追記:
242
+
243
+ おそらくDBやCursorをきちんと処理できていないために発生するエラーだと思います。
244
+
245
+ きちんとcursorやdbを扱っている部分の書き方をみなおす(多分お作法があると思うので)
246
+
247
+ https://qiita.com/YusukeIwaki/items/74b7d52ff8bd7d2849c9
248
+
249
+ create table sqlの書き方に問題がある場合が多いかもしれないらしいです。
250
+
251
+ https://forums.bignerdranch.com/t/cant-read-row-0-col-1-from-cursorwindow/11995
252
+
253
+
254
+
255
+ とりあえず一旦、AndroidManifest.xmlのandroid:allowBackup="false"にしてアプリを削除して入れ直してみて動くかどうか試してみてください。(アプリ削除かallowBackupか、どっちかだけで良いかもしれない?)
256
+
257
+
258
+
259
+ ちょっと根本問題解決かはわかりませんが、これで解決するのであれば一旦はこの質問でのやり取りはこの辺にしておきますか。。。(SQLiteの扱い方についてはまた別途)

2

コメントへの回答をさらに追記

2018/07/26 09:40

投稿

razuma
razuma

スコア1313

test CHANGED
@@ -159,3 +159,79 @@
159
159
  ```
160
160
 
161
161
  にしてidとtextをそれぞれで取り出して使えるようすると良いと思います。
162
+
163
+
164
+
165
+ さらに回答を追記:
166
+
167
+ 1.idsがどこにも定義されていないことが問題でした。
168
+
169
+ 2.queryTextsの戻り値にidsがセットされていませんでしたので、[0]にtexts配列、[1]にids配列が入るように修正
170
+
171
+ 3.show()の中でArray<MutableList<String>として受け取り、表示に使うtexts配列は[0]で受け取る、idsは[1]で受け取っておく。
172
+
173
+ 4.idsはnullチェックをする(nullチェックについてはkotolinとしてこの方法が最適かはわからないので別途調べてみてください)
174
+
175
+
176
+
177
+ ```
178
+
179
+ override fun onCreate(savedInstanceState: Bundle?) {
180
+
181
+ super.onCreate(savedInstanceState)
182
+
183
+ setContentView(R.layout.activity_main)
184
+
185
+
186
+
187
+ var ids:MutableList<String>? = null
188
+
189
+
190
+
191
+ fun show() {
192
+
193
+ //データベースに登録されている文字列の一覧を得る
194
+
195
+ val array = queryTexts(this)
196
+
197
+ val texts = array[0]
198
+
199
+ ids = array[1]
200
+
201
+ val listView = findViewById<ListView>(R.id.listView)
202
+
203
+ listView.adapter = ArrayAdapter<MutableList<String>>(this, R.layout.list_text_row, R.id.textView, texts)
204
+
205
+ }
206
+
207
+ // 略
208
+
209
+ selectedList.setOnItemClickListener { _, _, position, _ ->
210
+
211
+ // 略
212
+
213
+   if (ids != null) {
214
+
215
+ deleteItem(this, ids!![position])
216
+
217
+ }
218
+
219
+
220
+
221
+ fun queryTexts(context: Context) : Array<MutableList<String>> {
222
+
223
+ // 略
224
+
225
+ val resArray = arrayOf(texts, ids)
226
+
227
+ database.close()
228
+
229
+ return resArray
230
+
231
+ }
232
+
233
+ ```
234
+
235
+
236
+
237
+ 無事に動くと良いですが・・・。

1

コメントへの回答を追記

2018/07/25 15:52

投稿

razuma
razuma

スコア1313

test CHANGED
@@ -75,3 +75,87 @@
75
75
  }
76
76
 
77
77
  ```
78
+
79
+ コメントへの回答を追記:
80
+
81
+ ```
82
+
83
+ val ids = cursor.getString(cursor.getColumnIndex("id"))
84
+
85
+ texts.add(ids)
86
+
87
+ ```
88
+
89
+ のようにしてしまうとtextsの中にtextとidが混在してしまうので取り出したい値を取り出しにくくなります。
90
+
91
+ なので考えられる方法としては以下のようにMutableList<String>の配列で返すか
92
+
93
+ ```
94
+
95
+ fun queryTexts(context: Context) : Array<MutableList<String>> {
96
+
97
+ //略
98
+
99
+ val ids = mutableListOf<String>()
100
+
101
+ val texts = mutableListOf<String>()
102
+
103
+ cursor.use {
104
+
105
+ while(cursor.moveToNext()) {
106
+
107
+ val id = cursor.getString(cursor.getColumnIndex("id"))
108
+
109
+ ids.add(id)
110
+
111
+ val text = cursor.getString(cursor.getColumnIndex("text"))
112
+
113
+ texts.add(text)
114
+
115
+ }
116
+
117
+ }
118
+
119
+ val resArray = arrayOf(ids, texts)
120
+
121
+ return resArray
122
+
123
+ ```
124
+
125
+ 以下のようにクラスを作って返すか。
126
+
127
+ ```
128
+
129
+ data class ListData(val id: String, val text: String)
130
+
131
+
132
+
133
+ fun queryTexts(context: Context) : MutableList<ListData> {
134
+
135
+ //略
136
+
137
+ val listDatas = mutableListOf<ListData>()
138
+
139
+
140
+
141
+ cursor.use {
142
+
143
+ while(cursor.moveToNext()) {
144
+
145
+ val id = cursor.getString(cursor.getColumnIndex("id"))
146
+
147
+ val text = cursor.getString(cursor.getColumnIndex("text"))
148
+
149
+ listDatas.add(ListData(id, text))
150
+
151
+ }
152
+
153
+ }
154
+
155
+ return listDatas
156
+
157
+ }
158
+
159
+ ```
160
+
161
+ にしてidとtextをそれぞれで取り出して使えるようすると良いと思います。