布尔值作为SQLite数据库中的整数

时间:2013-07-20 15:53:09

标签: java android sqlite

我有一个“真或假”类型的测验。所以,有两个按钮,一个是真的,一个是带有问题的textview。我在Assets文件夹中导入了一个sqlite数据库。其中有4列:_id,question,correctAnswer,wrongAnswer。每列INTEGER除了问题是TEXT。因此,对于每个correctAnswer列,我设置0或1,具体取决于答案是真还是假。但是,在游戏中,无论问题是什么,我左边的TRUE按钮都错了,右边的FALSE按钮正确。我不知道我做错了什么。无论如何,这是代码:

public class Kviz extends Activity implements OnClickListener{

    Button true,false;
    TextView question;

    LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();

    private String generateWhereClause(){
        StringBuilder result = new StringBuilder();
        for (Long l : mAnsweredQuestions){
            result.append(" AND _ID <> " + l);
        }
        return result.toString();
    }

    private class Answer {
        public Answer(int opt, boolean correct) {
            option = opt;
            isCorrect = correct;
        }

        int option;
        boolean isCorrect;
    }

    Runnable mLaunchTask = new Runnable() {
        public void run() {
            nextQuestion();
        }
     };

    Handler mHandler = new Handler();

    final OnClickListener clickListener = new OnClickListener() {
        public void onClick(View v) {
            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
                mHandler.postDelayed(mLaunchTask,1200);
            }
            else{   
                Toast.makeText(getApplicationContext(), "Incorrect!", Toast.LENGTH_SHORT).show();
                mHandler.postDelayed(mLaunchTask,1200);
            }
        }
     };


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.kviz);

        inicijalizujVarijable();

        nextQuestion();
    }

    private void nextQuestion() {
        TestAdapter mDbHelper = new TestAdapter(this);
        DataBaseHelper myDbHelper = new DataBaseHelper(this);

        if(!myDbHelper.checkDataBase()){
        mDbHelper.createDatabase();
        }
        try{  

            mDbHelper.open(); 

            Cursor c = mDbHelper.getTestData(generateWhereClause());
            c.moveToFirst();

            mAnsweredQuestions.add(c.getLong(0));

            List<Answer> labels = new ArrayList<Answer>();

            if (c.getInt(2)==1){
        labels.add(new Answer(c.getInt(2), true));
        labels.add(new Answer(c.getInt(3), false));
        tacno.setTag(labels.get(0));
        netacno.setTag(labels.get(1));

        }else{
        labels.add(new Answer(c.getInt(2), false));
        labels.add(new Answer(c.getInt(3), true));
        netacno.setTag(labels.get(0));
        tacno.setTag(labels.get(1));
        }

            true.setOnClickListener(clickListener);
            false.setOnClickListener(clickListener);
        }

        finally{ 
            mDbHelper.close();
        }

    }

    private void inicijalizujVarijable() {

        true = (Button) findViewById(R.id.bTacno);
        false = (Button) findViewById(R.id.bNetacno);
        question = (TextView) findViewById(R.id.tvPitanje);

    }

    public void onClick(View v) {

    }

}

2 个答案:

答案 0 :(得分:1)

我将基于这段代码做出一些假设:

if (c.getInt(2)==1){
    labels.add(new Answer(c.getInt(2), true));
    labels.add(new Answer(c.getInt(3), false));
    tacno.setTag(labels.get(0));
    netacno.setTag(labels.get(1));
} else {
    labels.add(new Answer(c.getInt(2), false));
    labels.add(new Answer(c.getInt(3), true));
    netacno.setTag(labels.get(0));
    tacno.setTag(labels.get(1));
}

c.getInt(2)==1为真时......

  • 您为labels添加了两个答案,因此labels[0]=truelabels[1]=false
  • 您将tacno设置为true(索引0),将netacno设置为false(索引1)

c.getInt(2)==1为假...

  • 您为labels添加了两个答案,因此labels[0]=falselabels[1]=true
  • 您将netacno设置为false(索引0),将tacno设置为true(索引1)

在这两种情况下,tacno为真(正确),netacno为假(不正确)。你做了两次翻转。

答案 1 :(得分:0)

我不是百分百清楚,但在我看来,当你回到视图中时,屏幕上显示的任何旧的真/假按钮需要使用数据库中的值进行更新,否则它们将在xml布局中显示为默认设置。