2013-02-22 54 views
1

我正在开发一个测验。显示我的敬酒信息时发生错误。错误是,如果用户点击正确的答案,吐司说,即使我点击正确的选择,这是错误的答案。帮助真的很感激!这里是代码:Android吐司消息(使用单选按钮标识正确答案)

public class Question2 extends Activity { 
/** Called when the activity is first created. */ 


TextView question, items = null; 
RadioButton answer1 = null; 
RadioButton answer2 = null; 
RadioButton answer3 = null; 
RadioGroup answers = null; 
int selectedAnswer = -1; 
int quesIndex = 0; 
int numEvents = 0; 
int selected[] = null; 
int correctAns[] = null; 
boolean review = false; 
Button next = null; 
int score = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.startquiz); 

    try { 
     score = getIntent().getIntExtra("score",0); 
     items = (TextView)findViewById(R.id.displayitems); 
     question = (TextView) findViewById(R.id.displayquestion); 
     answer1 = (RadioButton) findViewById(R.id.option1); 
     answer2 = (RadioButton) findViewById(R.id.option2); 
     answer3 = (RadioButton) findViewById(R.id.option3); 
     answers = (RadioGroup) findViewById(R.id.QueGroup1); 


     next = (Button) findViewById(R.id.selected); 
     next.setOnClickListener(nextListener); 

     selected = new int[Question1.getQuesList().length()]; 
     java.util.Arrays.fill(selected, -1); 
     correctAns = new int[Question1.getQuesList().length()]; 
     java.util.Arrays.fill(correctAns, -1); 

     this.showQuestion(0, review); 



    } catch (Exception e) { 
     Log.e("", e.getMessage().toString(), e.getCause()); 
    } 

} 

private void showQuestion(int qIndex, boolean review) { 
    try { 
     JSONObject aQues = Question1.getQuesList().getJSONObject(
       qIndex); 
     String quesValue = aQues.getString("Question"); 
     if (correctAns[qIndex] == -1) { 
      String correctAnsStr = aQues.getString("CorrectAnswer"); 
      correctAns[qIndex] = Integer.parseInt(correctAnsStr); 
     } 

     question.setText(quesValue.toCharArray(), 0, quesValue.length()); 
     answers.check(-1); 
     answer1.setTextColor(Color.BLACK); 
     answer2.setTextColor(Color.BLACK); 
     answer3.setTextColor(Color.BLACK); 
     JSONArray ansList = aQues.getJSONArray("Answers"); 
     String aAns = ansList.getJSONObject(0).getString("Answer"); 
     answer1.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(1).getString("Answer"); 
     answer2.setText(aAns.toCharArray(), 0, aAns.length()); 
     aAns = ansList.getJSONObject(2).getString("Answer"); 
     answer3.setText(aAns.toCharArray(), 0, aAns.length()); 
     Log.d("", selected[qIndex] + ""); 
     if (selected[qIndex] == 0) 
      answers.check(R.id.option1); 
     if (selected[qIndex] == 1) 
      answers.check(R.id.option2); 
     if (selected[qIndex] == 2) 
      answers.check(R.id.option3); 

     setText(); 
     if (quesIndex == (Question1.getQuesList().length() - 1)) 
      next.setEnabled(false); 

     if (quesIndex < (Question1.getQuesList().length() - 1)) 
      next.setEnabled(true); 

     if (review) { 
      Log.d("review", selected[qIndex] + "" + correctAns[qIndex]); 
      ; 
      if (selected[qIndex] != correctAns[qIndex]) { 
       if (selected[qIndex] == 0) 
        answer1.setTextColor(Color.RED); 
       if (selected[qIndex] == 1) 
        answer2.setTextColor(Color.RED); 
       if (selected[qIndex] == 2) 
        answer3.setTextColor(Color.RED); 
      } 
      if (correctAns[qIndex] == 0) 
       answer1.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 1) 
       answer2.setTextColor(Color.GREEN); 
      if (correctAns[qIndex] == 2) 
       answer3.setTextColor(Color.GREEN); 
     } 
    } catch (Exception e) { 
     Log.e(this.getClass().toString(), e.getMessage(), e.getCause()); 
    } 
} 


private void setAnswer() { 
    if (answer1.isChecked()) 
     selected[quesIndex] = 0; 
    if (answer2.isChecked()) 
     selected[quesIndex] = 1; 
    if (answer3.isChecked()) 
     selected[quesIndex] = 2; 



    Log.d("", Arrays.toString(selected)); 
    Log.d("", Arrays.toString(correctAns)); 

} 

private OnClickListener nextListener = new OnClickListener() { 
    public void onClick(View v) { 

     for(int i=0; i<correctAns.length; i++){ 
      if ((correctAns[i] != -1) && (correctAns[i] == selected[i])) 
        { 

         score++; 
         Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show(); 
        }else 
        { 
        Toast.makeText(getApplicationContext(), "Your answer is wrong...", Toast.LENGTH_SHORT).show(); 
        } 
     } 


     quesIndex++; 
     try { 
      if (quesIndex >= Question1.getQuesList().length()) 
       quesIndex = Question1.getQuesList().length() - 1; 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     showQuestion(quesIndex, review); 

    } 
}; 
private void setText() throws JSONException { 
    this.setTitle("Question " + (quesIndex + 1) + " out of " 
      + Question1.getQuesList().length()); 
    items.setGravity(250); 
} 

public void reload() { 
    setAnswer(); 



Intent intent = getIntent(); 
overridePendingTransition(0, 0); 
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
finish(); 

overridePendingTransition(0, 0); 
startActivity(intent); 
} 
+3

调试与断点代码的,如果条件和检查correctAns [I]的价值观和的选择[I]是否符合要求的条件,我认为有条件问题或两者的值检查这些第一 – 2013-02-22 05:21:14

+3

什么是'correctAns'和'selected'的声明?如果它们都是'String []',那么你需要使用'equals()'进行比较,而不是'=='。 – 2013-02-22 05:27:28

+0

可以发布,你如何初始化并将其放在'correctAns'和'选择的' – Shoshi 2013-02-22 05:54:37

回答

0

看起来你可以改变你的状况。用户是否能够选择多个答案?
,并尝试用薄似条件

for(int i=0; i<correctAns.length; i++){ 
    if ((correctAns[i].euqalsIgnoreCase(selected[i]))){ 
     score++; 
     Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show(); 
    }else{ 
     Toast.makeText(getApplicationContext(), "Your answer is wrong...", Toast.LENGTH_SHORT).show(); 
    } 
} 

试试这个,让我知道它的工作与否。

+0

它不工作..我点击下一个按钮后仍然显示吐司消息。应该是当我点击答案(单选按钮) ,然后下一个按钮;吐司将显示如果我得到iinto下一个问题吐司将不再显示,除非我再次点击下一个按钮 – user2079544 2013-02-22 05:51:18

+0

值是否进入'selected []'你有没有检查过。我的意思是'selected [i]'从复选框中获取值。 – 2013-02-22 06:01:13

+0

请看我更新的代码.. – user2079544 2013-02-22 06:16:21

0

如果()条件应该是这样的: -

if ((correctAns[i] != -1) && (correctAns[i].equalsIgnoreCase(selected[i]))) { 
    score++; 
    Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show(); 
}else{ 
    Toast.makeText(getApplicationContext(), "Your answer is wrong...", Toast.LENGTH_SHORT).show(); 
} 
+0

你假设'correctAns'是一个String []',但是你写了'correctAns [i] == -1 '。请更新您的答案@amit singh – Shoshi 2013-02-22 05:47:01

+0

@Shoshi看到我的代码..ive更新它来澄清我的问题 – user2079544 2013-02-22 06:13:22