-
-
Save DanJ-01/978621cbbe90f0640aa2 to your computer and use it in GitHub Desktop.
Question class example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------- | |
// Question.java | |
//-------------- | |
package example.local.questionexample; | |
public class Question { | |
private String mPrompt; | |
private String mAnswer; | |
public Question(String prompt, String answer) { | |
mPrompt = prompt; | |
mAnswer = answer; | |
} | |
public String getPrompt() { | |
return mPrompt; | |
} | |
public String getAnswer() { | |
return mAnswer; | |
} | |
} | |
//---------------- | |
// TriviaBook.java | |
//---------------- | |
package example.local.questionexample; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TriviaBook { | |
private List<Question> mQuestions; | |
public TriviaBook() { | |
mQuestions = new ArrayList<Question>(); | |
//Add new questions here. | |
mQuestions.add(new Question("First question prompt", "answer to first question")); | |
mQuestions.add(new Question("Second question prompt", "answer to second question")); | |
} | |
public int getQuestionCount() { | |
return mQuestions.size(); | |
} | |
public Question getQuestionAt(int index) { | |
return mQuestions.get(index); | |
} | |
public String getAnswerToQuestionAt(int index) { | |
return mQuestions.get(index).getAnswer(); | |
} | |
public String getPromptToQuestionAt(int index) { | |
return mQuestions.get(index).getPrompt(); | |
} | |
} | |
//------------------ | |
// MainActivity.java | |
//------------------ | |
package example.local.questionexample; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.widget.TextView; | |
public class MainActivity extends ActionBarActivity { | |
private TriviaBook triviaBook; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
triviaBook = new TriviaBook(); | |
TextView promptTextView = (TextView)findViewById(R.id.promptTextView); | |
TextView answerTextView = (TextView)findViewById(R.id.answerTextView); | |
promptTextView.setText(triviaBook.getPromptToQuestionAt(0)); | |
answerTextView.setText(triviaBook.getAnswerToQuestionAt(0)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment