从Android中的Cursor获取下一个值

时间:2014-02-18 10:56:56

标签: android sqlite cursor

我有Option表,其中包含基于Question的行数。所以我想以行方式显示选项。我还希望onClick()事件检查Option是对还是错。

我的表格如下:答案1表示正确答案,0表示错误答案。

Option     QId      Answer
test1       1         0
test2       1         1
test3       1         0
test4       1         0

我的代码如下:

public Cursor getOptions(String QId) {
        try {
            String sql = "Select * from tblOptions Where QId=" + QId + "";

            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();
            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getOptions >>" + mSQLException.toString());
            throw mSQLException;
        }
    }
ConnectionAdapter dbCon = new ConnectionAdapter(this);
        dbCon.createDatabase();
        dbCon.open();

        Cursor question = dbCon.getQuestion();
        Cursor option = dbCon.getOptions(Utility
                .GetColumnValue(question, "QId"));

        dbCon.close();
        // SaveUserResponse();
        String Question = question.getString(question
                .getColumnIndex("Question"));

        tvNote.setText(Question);

        String Options = "";
        for (int i = 0; i < option.getCount(); i++) {
            Options = Options + Utility.GetColumnValue(option, "Option");
            option.moveToNext();
        }

        tvRQOption.setText(Options);

tvRQOption只向我显示test4。我做错了什么?我应该怎么做才能检查对错选项?

1 个答案:

答案 0 :(得分:1)

public Cursor getOptions(String QId) {
        try {
            String sql = "Select * from tblOptions Where QId=" + QId + "";

            Cursor mCur = mDb.rawQuery(sql, null);


        } catch (SQLException mSQLException) {
            Log.e(TAG, "getOptions >>" + mSQLException.toString());
            throw mSQLException;
        }
    }

从db获取值

Cursor cursor = db_obj.getoptions("Your_id")
{
 cursor.moveToFirst();
   if(cursor != null && cursor.getCount() > 0) 
   {
       String string = cursor.getString(cursor.getColumnIndex("Column_Name"));
       //Set this String value to Ui i.e your option
       cursor.moveToNext(); 
   }
}