英文:
Data saved not corresponding with database content SQLite litepal
问题
我对安卓开发不太熟悉。
我正在尝试从 `json字符串` 将数据保存到数据库
```java
[{
"question": "谁是“现代之爱”摇滚明星歌手?",
"imageUrl": "https://postimg.cc/2VL1Y1jd",
"answerOptions": [
"吉米·亨德里克斯",
"大卫·鲍伊",
"吉姆·莫里森",
"猫王"
],
"correctAnswer": "大卫·鲍伊"
}]
在 MainActivity
的 onCreate
中,我有:
for (int i = 0; i < list.size(); i++) {
Quiz quiz = list.get(i);
quiz.save();
}
这在调试时工作得很好,因此我调试时的情况是:
[![进入图片描述][1]][1]
但是当我查看数据库文件时,answerOptions
的子列表丢失了:
[![进入图片描述][2]][2]
因此,当我尝试在 runOnUiThread
中使用 litePal.findall
时,列表与 Quiz.class
不对应:
runOnUiThread(new Runnable() {
@Override
public void run() {
List<Quiz> list = LitePal.findAll(Quiz.class);
QuizAdapter quizAdapter = new QuizAdapter(list, MainActivity.this);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(quizAdapter);
}
});
class Quiz extends LitePalSupport {
String question;
String imageUrl;
String [] answerOptions;
String correctAnswer;
}
是否有可能的解决方案或替代方法?
代码部分已忽略。如果有其他需要翻译的内容,请继续提问。
<details>
<summary>英文:</summary>
I'm new to android development.
I'm trying to save data to the database from `json string`
[{
"question": "Who is the 'Modern Love' rock star singer?",
"imageUrl": "https://postimg.cc/2VL1Y1jd",
"answerOptions": [
"Jimi Hendrix",
"David Bowie",
"Jim Morrison",
"Elvis Presley"
],
"correctAnswer": "David Bowie"
}]
and in mainActivity `onCreate` I have:
for (int i = 0; i < list.size(); i++) {
Quiz quiz = list.get(i);
quiz.save();
}
which works perfectly well hence when I debug I have:
[![enter image description here][1]][1]
But when I view the database file, the subList of `answerOptions` is missing:
[![enter image description here][2]][2]
Thus when I try to `runOnUiThread`, the list at `litePal.findall` doesn't correspond with the `Quiz.class`
runOnUiThread(new Runnable() {
@Override
public void run() {
List<Quiz> list = LitePal.findAll(Quiz.class);
QuizAdapter quizAdapter = new QuizAdapter(list, MainActivity.this);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setAdapter(quizAdapter);
}
});
class Quiz extends LitePalSupport {
String question;
String imageUrl;
String [] answerOptions;
String correctAnswer;
}
Is there any possible solution/alternative to this approach?
[1]: https://i.stack.imgur.com/sYVEm.png
[2]: https://i.stack.imgur.com/OSA2O.png
</details>
# 答案1
**得分**: 0
我通过改变我的数据结构来解决这个问题:
```json
[{
"question": "谁是《现代爱情》摇滚明星歌手?",
"imageUrl": "https://postimg.cc/2VL1Y1jd",
"one": "吉米·亨德里克斯",
"two": "大卫·鲍伊",
"three": "吉姆·莫里森",
"four": "猫王",
"correctAnswer": "大卫·鲍伊"
}]
然后相应地调整了我的类:
public class Quiz extends LitePalSupport {
String question;
String imageUrl;
String one;
String two;
String three;
String four;
String correctAnswer;
}
然后在我的适配器中,我将各个字符串添加到数组中:
List<String> answerOptions = new ArrayList<>();
answerOptions.add(quiz.one);
answerOptions.add(quiz.two);
answerOptions.add(quiz.three);
answerOptions.add(quiz.four);
...
quizHolder.createRadioButtons(answerOptions.toArray(new String[0]));
...
英文:
I solved this by changing my data structure:
[{
"question": "Who is the 'Modern Love' rock star singer?",
"imageUrl": "https://postimg.cc/2VL1Y1jd",
"one": "Jimi Hendrix",
"two": "David Bowie",
"three": "Jim Morrison",
"four": "Elvis Presley",
"correctAnswer": "David Bowie"
}]
Then adjust my class accordingly:
public class Quiz extends LitePalSupport {
String question;
String imageUrl;
String one;
String two;
String three;
String four;
String correctAnswer;
}
Then in my adapter, I added the individual strings to an array
List<String> answerOptions = new ArrayList<>();
answerOptions.add(quiz.one);
answerOptions.add(quiz.two);
answerOptions.add(quiz.three);
answerOptions.add(quiz.four);
...
quizHolder.createRadioButtons(answerOptions.toArray(new String[0]));
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论