gpt4 book ai didi

java - 保存的数据与数据库内容不对应SQLite litepal

转载 作者:行者123 更新时间:2023-12-01 17:34:37 25 4
gpt4 key购买 nike

我是 Android 开发新手。

我正在尝试将数据从json字符串保存到数据库

[{
"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"
}]

在 mainActivity onCreate 中我有:

for (int i = 0; i < list.size(); i++) {
Quiz quiz = list.get(i);
quiz.save();
}

它工作得很好,因此当我调试时我有:

enter image description here

但是当我查看数据库文件时,answerOptions 的子列表丢失了:

enter image description here

因此,当我尝试 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;
}

这种方法有任何可能的解决方案/替代方案吗?

最佳答案

我通过更改数据结构解决了这个问题:

[{
"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"
}]

然后相应地调整我的类(class):

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]));
...

关于java - 保存的数据与数据库内容不对应SQLite litepal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61061928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com