数据保存的内容与数据库内容不对应 SQLite LitePal

huangapple go评论79阅读模式
英文:

Data saved not corresponding with database content SQLite litepal

问题

我对安卓开发不太熟悉。

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

```java
[{
	"question": "谁是“现代之爱”摇滚明星歌手?",
	"imageUrl": "https://postimg.cc/2VL1Y1jd",

	"answerOptions": [
		"吉米·亨德里克斯",
		"大卫·鲍伊",
		"吉姆·莫里森",
		"猫王"
	],

	"correctAnswer": "大卫·鲍伊"
}]

MainActivityonCreate 中,我有:

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&#39;m new to android development. 

I&#39;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",

&quot;answerOptions&quot;: [
	&quot;Jimi Hendrix&quot;,
	&quot;David Bowie&quot;,
	&quot;Jim Morrison&quot;,
	&quot;Elvis Presley&quot;
],

&quot;correctAnswer&quot;: &quot;David Bowie&quot;

}]

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&#39;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:

[{
	&quot;question&quot;: &quot;Who is the &#39;Modern Love&#39; rock star singer?&quot;,
	&quot;imageUrl&quot;: &quot;https://postimg.cc/2VL1Y1jd&quot;,
	&quot;one&quot;: &quot;Jimi Hendrix&quot;,
	&quot;two&quot;: &quot;David Bowie&quot;,
	&quot;three&quot;: &quot;Jim Morrison&quot;,
	&quot;four&quot;: &quot;Elvis Presley&quot;,
	&quot;correctAnswer&quot;: &quot;David Bowie&quot;
}]

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&lt;String&gt; answerOptions = new ArrayList&lt;&gt;();
answerOptions.add(quiz.one);
answerOptions.add(quiz.two);
answerOptions.add(quiz.three);
answerOptions.add(quiz.four);
...
quizHolder.createRadioButtons(answerOptions.toArray(new String[0]));
...

huangapple
  • 本文由 发表于 2020年4月6日 22:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定