2016-07-04 11 views
0

回答のある質問ごとに正しい回答者を登録したいのですが、いずれかの回答がクリックされた後、次の質問に移動します。私はまた、最後の質問に答えた直後にスコアを保持するつもりのtextviewのみを含む別のXMLファイルに正解の量を表示したい。オプションの下に4つのボタンがあります。アンドロイドクイズのアプリのスコアを追跡する

私はインターネット全体を通り過ぎてしまったと思います。私は本当に助けに感謝します:)

不明な点がある場合は、お気軽にお問い合わせください。

これは質問pictureID(クイズの国旗の場合)、オプションa、b、cおよび正解を保持する質問クラスです。私もスコアを保持しています。

 

public class Question { 

    private int bildID; 
    private String question; 
    private String optA; 
    private String optB; 
    private String optC; 
    private String answer; 
    private String score; 

    //Constructor 

    public Question(int bildID, String question, String optA, String optB, String optC, String answer) { 
     this.bildID = bildID; 
     this.question = question; 
     this.optA = optA; 
     this.optB = optB; 
     this.optC = optC; 
     this.answer = answer; 

    } //End konstruktor 

    //Get & Set method starts 
    public int getBildID() { 
     return bildID; 
    } 

    public void setBildID(int bildID) { 
     this.bildID = bildID; 
    } 

    public String getQuestion() { 
     return question; 
    } 

    public void setQuestion(String question) { 
     this.question = question; 
    } 

    public String getOptA() { 
     return optA; 
    } 

    public void setOptA(String optA) { 
     this.optA = optA; 
    } 

    public String getOptB() { 
     return optB; 
    } 

    public void setOptB(String optB) { 
     this.optB = optB; 
    } 

    public String getOptC() { 
     return optC; 
    } 

    public void setOptC(String optC) { 
     this.optC = optC; 
    } 

    public String getAnswer() { 
     return answer; 
    } 

    public void setAnswer(String answer) { 
     this.answer = answer; 
    } 

    public String getScore() { 
     return score; 
    } 

    public void setScore(String score) { 
     this.score = score; 
    } 
    //Get & Set methods end 

} //End konstruktor 

ここは主活動です。

 
    public class MainActivity extends AppCompatActivity {

//Method that's gonna help to check index postion and avoid outofbounds. private void fixIndexCountValue() { if (idx >= questionArray.size() - 1) { idx = questionArray.size() - 1; } else if (idx < 0) { idx = 0; } } //Creates an arraylist thats gonna hold questions, options, answers, pictureID. ArrayList<Question> questionArray = new ArrayList<>(); //Arraylist thats gonna hold the buttons ArrayList<Button> buttons = new ArrayList<>(); //Variable for index postion int idx = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Arraylists questionArray = new ArrayList<>(); buttons = new ArrayList<>(); //End //CODE FOR QUESTIONS Question q1 = new Question(R.drawable.australien, "What is the capital of Australia?", "Melbourne", "Chicago", "Sydney", "Canberra"); Question q2 = new Question(R.drawable.finland, "What country does this flag belong to?", "Sweden", "Denmark", "Norway", "Finland"); Question q3 = new Question(R.drawable.frankrike, "What is the biggest city in France?", "Lyon", "Marseille", "Bordeaux", "Paris"); Question q4 = new Question(R.drawable.jamaika, "What country does this flag belong to?", "Mexico", "Dominican Republic", "Cuba", "Jamaica"); Question q5 = new Question(R.drawable.kina, "What is the capital of China?", "Shanghai", "Hangzhou", "Guangzhou", "Beijing"); Question q6 = new Question(R.drawable.kurdistan, "What is the capital of Kurdistan?", "Kirkuk", "Dohuk", "Zakho", "Erbil"); Question q7 = new Question(R.drawable.sverige, "What is the capital of Sweden?", "Gothenburg", "Malmö", "Helsinki", "Stockholm"); Question q8 = new Question(R.drawable.norge, "What is the capital of Norway?", "Bergen", "Harstad", "Stockholm", "Oslo"); Question q9 = new Question(R.drawable.ryssland, "What city in Russia has the highest population?", "Saint Petersburg", "Omsk", "Samara", "Moskva"); Question q10 = new Question(R.drawable.tyskland, "What country does this flag belong to?", "Belgia", "Italy", "Austria", "Germany"); questionArray.add(q1); questionArray.add(q2); questionArray.add(q3); questionArray.add(q4); questionArray.add(q5); questionArray.add(q6); questionArray.add(q7); questionArray.add(q8); questionArray.add(q9); questionArray.add(q10); final Question question = questionArray.get(idx); //END CODE FOR QUESTIONS // Code that's gonna fill buttons, tv and imageview with data. final ImageView bild = (ImageView) findViewById(R.id.imageView); final TextView tv_question = (TextView) findViewById(R.id.tv_question); final Button btnAlt1 = (Button) findViewById(R.id.btnAlt1); final Button btnAlt2 = (Button) findViewById(R.id.btnAlt2); final Button btnAlt3 = (Button) findViewById(R.id.btnAlt3); final Button btnAlt4 = (Button) findViewById(R.id.btnAlt4); //Textview on the scorescreen thats going to display score final TextView tvscore = (TextView) findViewById(R.id.tvscore); //KNAPP SOM KOMMER UPP PÅ FÖRSTA SIDAN, STARTA QUIZ final Button btnStart = (Button) findViewById(R.id.btnStart); //Adds data from the Question class with get methods tv_question.setText(question.getQuestion()); bild.setImageResource(question.getBildID()); btnAlt1.setText(question.getOptA()); btnAlt2.setText(question.getOptB()); btnAlt3.setText(question.getOptC()); btnAlt4.setText(question.getAnswer()); //END //Adds the button id in the arraylist buttons.add(btnAlt1); buttons.add(btnAlt2); buttons.add(btnAlt3); buttons.add(btnAlt4); //End //CODE FOR CLICK FUNCTION ON EACH BUTTON btnAlt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { idx++; fixIndexCountValue(); Question questionClick = questionArray.get(idx); tv_question.setText(questionClick.getQuestion()); bild.setImageResource(questionClick.getBildID()); Collections.shuffle(buttons); buttons.get(0).setText(questionClick.getOptA()); buttons.get(1).setText(questionClick.getOptB()); buttons.get(2).setText(questionClick.getOptC()); buttons.get(3).setText(questionClick.getAnswer()); } }); //END BUTTON 1 btnAlt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { idx++; fixIndexCountValue(); Question questionClick = questionArray.get(idx); tv_question.setText(questionClick.getQuestion()); bild.setImageResource(questionClick.getBildID()); Collections.shuffle(buttons); buttons.get(0).setText(questionClick.getOptA()); buttons.get(1).setText(questionClick.getOptB()); buttons.get(2).setText(questionClick.getOptC()); buttons.get(3).setText(questionClick.getAnswer()); } }); //END BUTTON 2 btnAlt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { idx++; fixIndexCountValue(); Question questionClick = questionArray.get(idx); tv_question.setText(questionClick.getQuestion()); bild.setImageResource(questionClick.getBildID()); Collections.shuffle(buttons); buttons.get(0).setText(questionClick.getOptA()); buttons.get(1).setText(questionClick.getOptB()); buttons.get(2).setText(questionClick.getOptC()); buttons.get(3).setText(questionClick.getAnswer()); } }); //END BUTTON 3 btnAlt4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { idx++; fixIndexCountValue(); Question questionClick = questionArray.get(idx); tv_question.setText(questionClick.getQuestion()); bild.setImageResource(questionClick.getBildID()); Collections.shuffle(buttons); buttons.get(0).setText(questionClick.getOptA()); buttons.get(1).setText(questionClick.getOptB()); buttons.get(2).setText(questionClick.getOptC()); buttons.get(3).setText(questionClick.getAnswer()); } });//END BUTTON 4 } //End oncreate } //End main

+0

を主な活動" ? –

+0

申し訳ありません、それを編集するつもりです。私はすべての質問に答えた後、正しい回答者を登録したいということでした。選択肢の1つがクリックされた後、次の質問に移動します。また、最後の質問に答えた直後にスコアを保持するテキストビューのみを含む別のxmlファイルに正解の量を表示したい。 –

+0

あなたが私にプロジェクトを送ってもらえない限り、問題を見つけるのは難しいです。私はすぐに問題を見つけて実行することができます。 –

答えて

0

私はあなたのMainActivityでこのメソッドを追加したhere

から、あなたの更新プロジェクトをダウンロード:あなたは正しい答えを登録するには問題」とはどういう意味ですか

private void showNextQuestionAndSaveAnswer(String answer) 
{ 

    if (answer == null) 
     return; 

    if (questionArray.get(idx).getAnswer().equalsIgnoreCase(answer)) 
    { 
     questionArray.get(idx).isCorrect = true; 
    } 

    idx++; 

    if (idx < questionArray.size()) { 
     fixIndexCountValue(); 
     Question questionClick = questionArray.get(idx); 
     tv_question.setText(questionClick.getQuestion()); 
     bild.setImageResource(questionClick.getBildID()); 
     Collections.shuffle(buttons); 
     buttons.get(0).setText(questionClick.getOptA()); 
     buttons.get(1).setText(questionClick.getOptB()); 
     buttons.get(2).setText(questionClick.getOptC()); 
     buttons.get(3).setText(questionClick.getAnswer()); 
    } 
    else 
    { 
     Intent resultIntent = new Intent(MainActivity.this,ResultActivity.class); 
     resultIntent.putExtra("list",questionArray); 
     startActivity(resultIntent); 
    } 
} 
関連する問題