2016-04-14 58 views
0

所以我是新來的java和我試圖讓測驗應用程序作爲練習。我有點發一個,但它不工作:equals方法在比較TextView字符串時不起作用

public class QuizActivity extends AppCompatActivity { 
TextView QuestionText; 
Button button1; 
Button button2; 
Button button3; 
Button button4; 
ArrayList<Question> listOfQuestions; 
int currentQuestion = 0; 
Context context = this; 
int NumberOfQuestions; 
GameCreator game; 
String totalCorrect = ""; 
String totalWrong = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 

    QuestionText = (TextView) findViewById(R.id.textJautajums); 
    button1 = (Button) findViewById(R.id.buttonOpcija1); 
    button2 = (Button) findViewById(R.id.buttonOpcija2); 
    button3 = (Button) findViewById(R.id.buttonOpcija3); 
    button4 = (Button) findViewById(R.id.buttonOpcija4); 
    NumberOfQuestions = Integer.parseInt(context.getString(R.string.JautajumuSkaits).toString()); 
    game = new GameCreator(NumberOfQuestions); 
    listOfQuestions = game.makeQuestions(); 

    Resources r = getResources(); 
    int px1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 165, r.getDisplayMetrics()); 
    button1.setWidth(px1); 
    button2.setWidth(px1); 
    button3.setWidth(px1); 
    button4.setWidth(px1); 

    Resources e = getResources(); 
    int px2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 125, r.getDisplayMetrics()); 
    button1.setHeight(px2); 
    button2.setHeight(px2); 
    button3.setHeight(px2); 
    button4.setHeight(px2); 


    if (currentQuestion == 0){ 
     setQuestion(listOfQuestions.get(0)); 
    } 

    button1.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View V){ 
        gajiens(button1.getText().toString(), listOfQuestions.get(currentQuestion)); 
        currentQuestion++; 
       } 
      } 
    ); 

    button2.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View V){ 
        gajiens(button2.getText().toString(), listOfQuestions.get(currentQuestion)); 
        currentQuestion++; 
       } 
      } 
    ); 

    button3.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View V){ 
        gajiens(button3.getText().toString(), listOfQuestions.get(currentQuestion)); 
        currentQuestion++; 
       } 
      } 
    ); 

    button4.setOnClickListener(
      new Button.OnClickListener(){ 
       public void onClick(View V){ 
        gajiens(button4.getText().toString(), listOfQuestions.get(currentQuestion)); 
        currentQuestion++; 
       } 
      } 
    ); 


} 

public void gajiens(String answer, Question thisQuestion){ 
    if (currentQuestion < 14){ 

     if (answer.equals(thisQuestion.getAnswer())){ 
      totalCorrect += "Question: " + thisQuestion.getQuestion() + "\nYour Answer: " + answer + "\n"; 
     } else { 
      totalWrong += "Question: " + thisQuestion.getQuestion()) + "\nYour Answer: " + answer + "\n"; 
     } 

     currentQuestion++; 
     setQuestion(listOfQuestions.get(currentQuestion)); 
    } else { 
     Intent intent = new Intent(this, EndActivity.class); 
     intent.putExtra("correct", totalCorrect); 
     intent.putExtra("wrong", totalWrong); 
     startActivity(intent); 
    } 
} 

public void setQuestion(Question kursh){ 
    QuestionText.setText(kursh.getJautajums()); 
    button1.setText(kursh.getOption1()); 
    button2.setText(kursh.getOption2()); 
    button3.setText(kursh.getOption3()); 
    button4.setText(kursh.getOption4()); 
} 

對象的問題是:

public Question(String Question, String Option1, String Option2, String Option3,String Option4, String correctAnswer){ 
    Question = Question; 
    Option1 = Option1; 
    Option2 = Option2; 
    Option3 = Option3; 
    Option4 = Option4; 
    correctAnswer = correctAnswer; 
} 

基本問題是,應用程序不計算正確的答案。出於某種原因,它大部分時間都使用TextView的原始文本作爲「correctAnswer」。任何人都知道該怎麼做?我懷疑這是不正常工作,這不是特別好的方法,所以也許有人可以建議一個更好的?

+1

也許你應該改變你的問題類澄清,你把它的參數,而不是重新分配他們的TextView用字符串的值。例如,'this.Question = Question;'。另外,變量是camelCased的,請不要使用大寫變量名稱。 –

+0

.equals可能起作用,可能不起作用的是邏輯/設置,它可讓您訪問.equals :)注意,在您的問題構造函數中,您可能想要在java中查看'this'的用途。 – zgc7009

+1

「問題」的構造函數沒有設置任何字段。它將參數重新分配給它們自己的值。 – f1sh

回答

0

來比較,你可以做到這一點,如下

TextView tvAnswer = (TextView) findViewById(R.id.tvAnswer); 
String correctAnswer = "Correct Answer"; 
if(correctAnswer.equals(tvAnswer.getText.toString())) 
{ 
    //do something 
} 
else{ 
    //do something 
    }