0

我有一种方法可以从数据库答案表中获取答案 ID(特定于每个答案)-getAnswerId()

但我想改变它。我在问题数据库中创建了 answer0,answer1, answer2 列,并希望将 0,1,2 作为 answerId 。如何为 getAnswerId() 识别它?

不久;

列名:answer0,answer1,answer2

取“0,1,2”作为“ answerId ”并放入“ getAnswerId()

然后我想像这样使用它:

公共 int getAnswerId() {

        return this.answerId;
}

编辑:

sqlhelper:

final String query = "SELECT " +
            "answer._id, answerlang.AnswerText, answer.Correct, " +
            "answersquestions.QuestionID" +
            " FROM answersquestions" +
            " INNER JOIN answerlang" +
            " ON answersquestions.AnswerID = answerlang.AnswerID" +
            " INNER JOIN answer" +
            " ON answerlang.AnswerID = answer._id" +
            " WHERE answersquestions.QuestionID IN ( " +
            allQuestionIds +
            " )  ORDER BY answersquestions.QuestionID ";
    this.cursor = this.db.rawQuery(query, null);

    if (this.cursor.moveToFirst()) {
        do {
            for (final Question q : questions) {
                if (q.getQuestionId() == this.cursor.getInt(3)) {
                    q.addAnswer(new Answer(this.cursor.getInt(0),
                            this.cursor.getString(1),
                            (this.cursor.getInt(2) == 1 ? true : false)));
                }
            }
        } while (this.cursor.moveToNext());
    }
    this.cursor.close();

this.cursor.getInt(0) 接受 answer._id (_ids from answer table)

我更改了数据库结构并将 answer0、answer 1、answer 2 添加到 answerlang 表中。所以我需要一张表中每个答案的 id 。我想取QuestionID0(或 1,2)(来自 answer0,1,2)所以answerID = QuestionID+0对每个答案都是特别的

问题是如何间隔QuestionID+0去放置,而不是this.cursor.getString(0)

// // // // \ \ \ \

我的问题总结:

 use **this.sample.getInt()** instead of this.cursor.getInt(0)
for example:
    
    this.sample.getInt() : will have interval 10,11,12 for question 1 (QuestionID=1, answer0=0 so answer0 id = 10, answer1 id = 11, answer2 id = 12  as spesific id )
    
     will have interval 20,21,22 for question 2 (QuestionID=2, answer0=0 so answer0 id = 20 ...... as spesific id )
4

2 回答 2

0

假设当questionId=1 和 answerFromDB="answer1"你可以做的是首先将问题 id 传递给方法,然后将 questionId 和 answerID 中的整数联系到一个新字符串中并将其解析为一个整数,因此结果将是11在这种情况下。如果questionId=1 和 answerFromDB="answer2"那么 answerid 将为12等等。

public int getAnswerId( String questionId) {
//here questionId=0 or 1 or 2 etc in string format 
String newAnswerId=questionId+answerFromDB.substring(8) 
 return Integer.parseInt(newAnswerId);
    }
于 2013-03-19T05:51:46.427 回答
0

您可能必须使用

Integer.parseInt(answerFromDB.substring(8));
"answerid0".substring(8) = 0
"answerid1".substring(8) = 1

ETC

于 2013-03-18T11:01:02.530 回答