0

我有一个“真假”类型的测验。所以,两个按钮,一个真一个假,一个带问题的文本视图。我在 Assets 文件夹中导入了一个 sqlite 数据库。其中有 4 列:_id、问题、正确答案、错误答案。除了问题是 TEXT 之外,每一列都是 INTEGER。因此,对于每个正确答案列,我设置 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) {

    }

}
4

2 回答 2

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, solabels[0]=truelabels[1]=false
  • 您设置tacnotrue(索引 0)和netacnofalse索引 1)

什么时候c.getInt(2)==1假...

  • 您将两个答案添加到labels, solabels[0]=falselabels[1]=true
  • 您设置netacnofalse(索引 0)和tacnotrue索引 1)

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

于 2013-07-30T19:35:23.313 回答
0

我不是 100% 清楚,但在我看来,当您返回视图时,屏幕上显示的任何旧 true/fals 按钮都需要使用数据库中的值进行更新,否则它们会显示作为它们在 xml 布局中的默认设置。

于 2013-07-30T19:06:49.043 回答