Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay score in TextView in android?
    primarykey
    data
    text
    <p>I am working on android quiz. I have <strong>setQuestion()</strong> method in <strong>QuestionActivity</strong> class. I have question.xml in which i create <strong>TextView</strong> to show scores. I want to write code in setQuestion so that its set TextView of that button with score.Basically my scores are retrieving from getScore() method in GamePlay class and scores updated with right or wrong answer. like in setQuestion() method my next question answer TextView updated on next round. </p> <pre><code>public class GamePlay { private int numRounds; private int difficulty; private int score; private int round; private List&lt;Question&gt; questions = new ArrayList&lt;Question&gt;(); /** * @return the right */ public int getScore() { return score; } /** * @param right the right to set */ public void setScore(int score) { this.score = score; } /** * @return the round */ public int getRound() { return round; } /** * @param round the round to set */ public void setRound(int round) { this.round = round; } /** * @param difficulty the difficulty to set */ public void setDifficulty(int difficulty) { this.difficulty = difficulty; } /** * @return the difficulty */ public int getDifficulty() { return difficulty; } /** * @param questions the questions to set */ public void setQuestions(List&lt;Question&gt; questions) { this.questions = questions; } /** * @param q the question to add */ public void addQuestions(Question q) { this.questions.add(q); } /** * @return the questions */ public List&lt;Question&gt; getQuestions() { return questions; } public Question getNextQuestion(){ //get the question Question next = questions.get(this.getRound()); //update the round number to the next round this.setRound(this.getRound()+1); return next; } /** * method to increment the number of correct answers this game */ /** * method to increment the number of incorrect answers this game */ public void incrementScore(){ score=score+100; } public void decrementScore() { score=score-50; } /** * @param numRounds the numRounds to set */ public void setNumRounds(int numRounds) { this.numRounds = numRounds; } /** * @return the numRounds */ public int getNumRounds() { return numRounds; } /** * method that checks if the game is over * @return boolean */ public boolean isGameOver(){ return (getRound() &gt;= getNumRounds()); } } public class QuestionActivity extends Activity implements OnClickListener{ private Question currentQ; private GamePlay currentGame; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.question); /** * Configure current game and get question */ currentGame = ((ABCApplication)getApplication()).getCurrentGame(); currentQ = currentGame.getNextQuestion(); Button nextBtn1 = (Button) findViewById(R.id.answer1); nextBtn1.setOnClickListener(this); Button nextBtn2 = (Button) findViewById(R.id.answer2); nextBtn2.setOnClickListener(this); Button nextBtn3 = (Button) findViewById(R.id.answer3); nextBtn3.setOnClickListener(this); Button nextBtn4 = (Button) findViewById(R.id.answer4); nextBtn4.setOnClickListener(this); /** * Update the question and answer options.. */ setQuestions(); } /** * Method to set the text for the question and answers from the current games * current question */ private void setQuestions() { //set the question text from current question String question = Utility.capitalise(currentQ.getQuestion()); TextView qText = (TextView) findViewById(R.id.question); qText.setText(question); //set the available options List&lt;String&gt; answers = currentQ.getQuestionOptions(); TextView option1 = (TextView) findViewById(R.id.answer1); option1.setText(Utility.capitalise(answers.get(0))); TextView option2 = (TextView) findViewById(R.id.answer2); option2.setText(Utility.capitalise(answers.get(1))); TextView option3 = (TextView) findViewById(R.id.answer3); option3.setText(Utility.capitalise(answers.get(2))); TextView option4 = (TextView) findViewById(R.id.answer4); option4.setText(Utility.capitalise(answers.get(3))); TextView score1=(TextView) findViewById(R.id.score); setText score1=GamePlay.getScore(); } @Override public void onClick(View arg0) { //Log.d("Questions", "Moving to next question"); if(!checkAnswer(arg0)) return; /** * check if end of game */ if (currentGame.isGameOver()){ //Log.d("Questions", "End of game! lets add up the scores.."); //Log.d("Questions", "Questions Correct: " + currentGame.getRight()); //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong()); Intent i = new Intent(this, EndgameActivity.class); startActivity(i); finish(); } else{ Intent i = new Intent(this, QuestionActivity.class); startActivity(i); finish(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK : return true; } return super.onKeyDown(keyCode, event); } /** * Check if a checkbox has been selected, and if it * has then check if its correct and update gamescore */ private boolean checkAnswer(View v) { Button b=(Button) v; String answer = b.getText().toString(); //Log.d("Questions", "Valid Checkbox selection made - check if correct"); if (currentQ.getAnswer().equalsIgnoreCase(answer)) { //Log.d("Questions", "Correct Answer!"); currentGame.incrementScore(); } else{ //Log.d("Questions", "Incorrect Answer!"); currentGame.decrementScore(); } return true; } question.xml &lt;TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/score_img"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload