Parse.com挂起了一对多的关系

时间:2015-07-08 11:07:35

标签: android database parse-platform one-to-many

我需要在Parse中保存一对多的关系,每个Cathegory必须有几个问题(最多4个),每个问题都必须有问题和答案的字符串(最多5个字符串)和另外4个图像(尚未实现) )我需要它在线更新“播出”。我目前正在测试数据库服务(现在我决定使用Parse,但我需要保存Cathegory和Question之间的关系)

这是我的Activity(片段)代码:

sight = GameObject.Find("Player/Sight");

类别是正确创建的,但如果我查看问题,那么只有: Parse Problem Image

问题类代码:

            Question q1 = new Question("How much money cost new Skoda Fabia?", "300 " +
                    "000kc", "200 000kc", "100 000kc", "50 000kc");
            q1.saveInBackground();
            Question q2 = new Question("How much money cost new Skoda Felicia?", "300 " +
                    "000kc", "200 000kc", "100 000kc", "150 000kc");
            q2.saveInBackground();
            Question q3 = new Question("How much money cost new Skoda Octavia?", "500 " +
                    "000kc", "150 000kc", "100 000kc", "50 000kc");
            q3.saveInBackground();

            ArrayList<Question> questionArrayList = new ArrayList<>();
            questionArrayList.add(q1);
            questionArrayList.add(q2);
            questionArrayList.add(q3);

            ParseObject parseObject = new ParseObject("Cathegory");
            parseObject.put("Auto", questionArrayList);
            parseObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    Log.d(TAG,"Save was sucesfully");
                    Log.d(TAG,"error was ? : " + (e == null ? "none" : e.getMessage()));
                }
            });

}

1 个答案:

答案 0 :(得分:0)

愚蠢的错误。请注意这一点。

问题类的正确代码:

 @ParseClassName("Question")
public class Question extends ParseObject {
public static final String TAG = Question.class.getName();

private String question;
private String rightAnswer;
private String wrongAnswer1;
private String wrongAnswer2;
private String wrongAnswer3;

public Question(){
    //need for Parse
}

public Question(String question, String rightAnswer, String wrongAnswer1, String wrongAnswer2, String wrongAnswer3) {
    super();
    super.put("question", question);
    super.put("rightAnswer", rightAnswer);
    super.put("wrongAnswer1", wrongAnswer1);
    super.put("wrongAnswer2", wrongAnswer2);
    super.put("wrongAnswer3", wrongAnswer3);
    this.question = question;
    this.rightAnswer = rightAnswer;
    this.wrongAnswer1 = wrongAnswer1;
    this.wrongAnswer2 = wrongAnswer2;
    this.wrongAnswer3 = wrongAnswer3;
}

public Question(String theClassName, String question, String rightAnswer, String wrongAnswer1, String wrongAnswer2, String wrongAnswer3) {
    super(theClassName);
    super.put("question", question);
    super.put("rightAnswer", rightAnswer);
    super.put("wrongAnswer1", wrongAnswer1);
    super.put("wrongAnswer2", wrongAnswer2);
    super.put("wrongAnswer3", wrongAnswer3);
    this.question = question;
    this.rightAnswer = rightAnswer;
    this.wrongAnswer1 = wrongAnswer1;
    this.wrongAnswer2 = wrongAnswer2;
    this.wrongAnswer3 = wrongAnswer3;
}

}