Slaine82
Newbie
Hi there,
I am building a quiz application and I have two classes:-
Main class which holds the menu for the application and the main parts of the code
Question class which creates and sets up a question, and hold methods for evaluating the questions answer
The main class has a method public void setQuestion(int qNo) which creates a new Question object. The thing is once I have created the question and evaluated it which happens in the Question class, from this class I want to call the setQuestion method back in the Main class, is this possible?
Below are my two classes:-
MAIN
QUESTION
and I have bolded where I want to call the function from the Question class, any help would be much appreciated.
I am building a quiz application and I have two classes:-
Main class which holds the menu for the application and the main parts of the code
Question class which creates and sets up a question, and hold methods for evaluating the questions answer
The main class has a method public void setQuestion(int qNo) which creates a new Question object. The thing is once I have created the question and evaluated it which happens in the Question class, from this class I want to call the setQuestion method back in the Main class, is this possible?
Below are my two classes:-
MAIN
Code:
package com.zapp.potogold;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity {
int currentQ = 0;
Context currentContext;
Question question;
Button submitButton;
Resources res;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
currentContext = this;
}
public void setView(View view){
switch(view.getId()){
case R.id.menuitem_1:
//activate the quiz layout
setContentView(R.layout.quiz);
setQuestion(currentQ);
break;
case R.id.menuitem_2:
break;
}
}
public void setQuestion(int qNo){
submitButton = (Button) findViewById(R.id.submit);
submitButton.setEnabled(false);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitClicked();
}
});
question = new Question(currentContext, qNo);
}
public void submitClicked(){
question.evaluateAnswer();
}
}
Code:
package com.zapp.potogold;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Question {
private Context mContext;
private TextView tv;
Resources res;
RadioButton radioButton;
RadioButton selectedRadioButton;
RadioGroup rg;
Button submitButton;
Button nextQButton;
int currentQNo;
static int quizPoints = 0;
//constructor
public Question(Context ctx, int qNumber) {
mContext = ctx;
setQStem(qNumber);
setAnswers(qNumber);
submitButton = (Button) ((Activity) mContext).findViewById(R.id.submit);
currentQNo = qNumber;
}
public void setQStem(int qNumber){
//set the question text
tv = (TextView)((Activity) mContext).findViewById(R.id.questionView);
res = mContext.getResources();
String[] questions = res.getStringArray(R.array.questions);
String qText = questions[qNumber];
tv.setText(qText);
}
public void setAnswers(int qNumber){
String[] answers = res.getStringArray(R.array.qAnswers);
String currentQAnswers = answers[qNumber];
String[] separated = currentQAnswers.split("@");
rg = (RadioGroup)((Activity) mContext).findViewById(R.id.radioGroup1);
for(int a = 0; a < separated.length; a++){
radioButton = new RadioButton(mContext);
radioButton.setText(separated[a]);
radioButton.setId(a);
rg.addView(radioButton);
radioButton.setOnClickListener(radio_listener);
}
}
public void evaluateAnswer(){
String[] correctAnswers = res.getStringArray(R.array.correctAnswers);
String currentQCorAns = correctAnswers[currentQNo];
String currentSelectedString = (String)(selectedRadioButton.getText());
if(currentSelectedString.equals(currentQCorAns)){
quizPoints += 2;
setResults("correct");
} else {
setResults("incorrect");
}
}
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
submitButton.setEnabled(true);
// Perform action on clicks
selectedRadioButton = (RadioButton) v;
}
};
private void setResults(String result){
((Activity) mContext).setContentView(R.layout.results);
String nextText = ((Activity) mContext).getString(R.string.result_next);
TextView correctTextView = new TextView(mContext);
TextView scoreTextView = new TextView(mContext);
TextView nextTextView = new TextView(mContext);
correctTextView = (TextView)((Activity) mContext).findViewById(R.id.resultText1);
scoreTextView = (TextView)((Activity) mContext).findViewById(R.id.resultScore);
nextTextView = (TextView)((Activity) mContext).findViewById(R.id.resultText2);
nextTextView.setText(nextText);
scoreTextView.setText(Integer.toString(quizPoints));
nextTextView.setText(nextText);
if(result == "correct"){
String correctText = ((Activity) mContext).getString(R.string.result_correct);
correctTextView.setText(correctText);
} else {
String incorrectText = ((Activity) mContext).getString(R.string.result_incorrect);
correctTextView.setText(incorrectText);
}
nextQButton = (Button)((Activity) mContext).findViewById(R.id.nextQButton);
nextQButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
[B]//I want to call the setQuestion function in the main class to call the next question[/B]
}
});
}
}