回答編集履歴
3
コメントに対する回答2
answer
CHANGED
@@ -116,4 +116,15 @@
|
|
116
116
|
}
|
117
117
|
```
|
118
118
|
|
119
|
-
無事に動くと良いですが・・・。
|
119
|
+
無事に動くと良いですが・・・。
|
120
|
+
|
121
|
+
さらにさらにコメントに回答を追記:
|
122
|
+
おそらくDBやCursorをきちんと処理できていないために発生するエラーだと思います。
|
123
|
+
きちんとcursorやdbを扱っている部分の書き方をみなおす(多分お作法があると思うので)
|
124
|
+
https://qiita.com/YusukeIwaki/items/74b7d52ff8bd7d2849c9
|
125
|
+
create table sqlの書き方に問題がある場合が多いかもしれないらしいです。
|
126
|
+
https://forums.bignerdranch.com/t/cant-read-row-0-col-1-from-cursorwindow/11995
|
127
|
+
|
128
|
+
とりあえず一旦、AndroidManifest.xmlのandroid:allowBackup="false"にしてアプリを削除して入れ直してみて動くかどうか試してみてください。(アプリ削除かallowBackupか、どっちかだけで良いかもしれない?)
|
129
|
+
|
130
|
+
ちょっと根本問題解決かはわかりませんが、これで解決するのであれば一旦はこの質問でのやり取りはこの辺にしておきますか。。。(SQLiteの扱い方についてはまた別途)
|
2
コメントへの回答をさらに追記
answer
CHANGED
@@ -78,4 +78,42 @@
|
|
78
78
|
return listDatas
|
79
79
|
}
|
80
80
|
```
|
81
|
-
にしてidとtextをそれぞれで取り出して使えるようすると良いと思います。
|
81
|
+
にしてidとtextをそれぞれで取り出して使えるようすると良いと思います。
|
82
|
+
|
83
|
+
さらに回答を追記:
|
84
|
+
1.idsがどこにも定義されていないことが問題でした。
|
85
|
+
2.queryTextsの戻り値にidsがセットされていませんでしたので、[0]にtexts配列、[1]にids配列が入るように修正
|
86
|
+
3.show()の中でArray<MutableList<String>として受け取り、表示に使うtexts配列は[0]で受け取る、idsは[1]で受け取っておく。
|
87
|
+
4.idsはnullチェックをする(nullチェックについてはkotolinとしてこの方法が最適かはわからないので別途調べてみてください)
|
88
|
+
|
89
|
+
```
|
90
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
91
|
+
super.onCreate(savedInstanceState)
|
92
|
+
setContentView(R.layout.activity_main)
|
93
|
+
|
94
|
+
var ids:MutableList<String>? = null
|
95
|
+
|
96
|
+
fun show() {
|
97
|
+
//データベースに登録されている文字列の一覧を得る
|
98
|
+
val array = queryTexts(this)
|
99
|
+
val texts = array[0]
|
100
|
+
ids = array[1]
|
101
|
+
val listView = findViewById<ListView>(R.id.listView)
|
102
|
+
listView.adapter = ArrayAdapter<MutableList<String>>(this, R.layout.list_text_row, R.id.textView, texts)
|
103
|
+
}
|
104
|
+
// 略
|
105
|
+
selectedList.setOnItemClickListener { _, _, position, _ ->
|
106
|
+
// 略
|
107
|
+
if (ids != null) {
|
108
|
+
deleteItem(this, ids!![position])
|
109
|
+
}
|
110
|
+
|
111
|
+
fun queryTexts(context: Context) : Array<MutableList<String>> {
|
112
|
+
// 略
|
113
|
+
val resArray = arrayOf(texts, ids)
|
114
|
+
database.close()
|
115
|
+
return resArray
|
116
|
+
}
|
117
|
+
```
|
118
|
+
|
119
|
+
無事に動くと良いですが・・・。
|
1
コメントへの回答を追記
answer
CHANGED
@@ -36,4 +36,46 @@
|
|
36
36
|
db.delete("texts", "id = ?", args)
|
37
37
|
}
|
38
38
|
}
|
39
|
-
```
|
39
|
+
```
|
40
|
+
コメントへの回答を追記:
|
41
|
+
```
|
42
|
+
val ids = cursor.getString(cursor.getColumnIndex("id"))
|
43
|
+
texts.add(ids)
|
44
|
+
```
|
45
|
+
のようにしてしまうとtextsの中にtextとidが混在してしまうので取り出したい値を取り出しにくくなります。
|
46
|
+
なので考えられる方法としては以下のようにMutableList<String>の配列で返すか
|
47
|
+
```
|
48
|
+
fun queryTexts(context: Context) : Array<MutableList<String>> {
|
49
|
+
//略
|
50
|
+
val ids = mutableListOf<String>()
|
51
|
+
val texts = mutableListOf<String>()
|
52
|
+
cursor.use {
|
53
|
+
while(cursor.moveToNext()) {
|
54
|
+
val id = cursor.getString(cursor.getColumnIndex("id"))
|
55
|
+
ids.add(id)
|
56
|
+
val text = cursor.getString(cursor.getColumnIndex("text"))
|
57
|
+
texts.add(text)
|
58
|
+
}
|
59
|
+
}
|
60
|
+
val resArray = arrayOf(ids, texts)
|
61
|
+
return resArray
|
62
|
+
```
|
63
|
+
以下のようにクラスを作って返すか。
|
64
|
+
```
|
65
|
+
data class ListData(val id: String, val text: String)
|
66
|
+
|
67
|
+
fun queryTexts(context: Context) : MutableList<ListData> {
|
68
|
+
//略
|
69
|
+
val listDatas = mutableListOf<ListData>()
|
70
|
+
|
71
|
+
cursor.use {
|
72
|
+
while(cursor.moveToNext()) {
|
73
|
+
val id = cursor.getString(cursor.getColumnIndex("id"))
|
74
|
+
val text = cursor.getString(cursor.getColumnIndex("text"))
|
75
|
+
listDatas.add(ListData(id, text))
|
76
|
+
}
|
77
|
+
}
|
78
|
+
return listDatas
|
79
|
+
}
|
80
|
+
```
|
81
|
+
にしてidとtextをそれぞれで取り出して使えるようすると良いと思います。
|