• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem Getting and Setting Public Variables

TCJumpman

Lurker
Jul 28, 2014
3
0
Hello, in an app i'm making i'm trying to set variables to another class when i'm making intents to move to that Activity.

Here's the Activity where I try the setQuestionNumber() and setNumberOfQuestions() while creating intents.
[HIGH]
public static final String TAG = MainActivity.class.getSimpleName();
private CreateQuizActivity createQuizActivity;

protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
createQuizActivity = new CreateQuizActivity();
switch(which){
case 0: // 1 question
Intent intent = new Intent(MainActivity.this, CreateQuizActivity.class);
startActivity(intent);
createQuizActivity.setNumberOfQuestions(1);
createQuizActivity.setQuestionNumber(1);
break;
case 1: // 2 questions
Intent intent2 = new Intent(MainActivity.this, CreateQuizActivity.class);
startActivity(intent2);
createQuizActivity.setNumberOfQuestions(2);
createQuizActivity.setQuestionNumber(1);
break;
case 2: // 3 questions
createQuizActivity.setNumberOfQuestions(3);
createQuizActivity.setQuestionNumber(1);
Intent intent3 = new Intent(MainActivity.this, CreateQuizActivity.class);
startActivity(intent3);
break;
case 3: // 4 questions
createQuizActivity.setNumberOfQuestions(4);
createQuizActivity.setQuestionNumber(1);
Intent intent4 = new Intent(MainActivity.this, CreateQuizActivity.class);
startActivity(intent4);
break;
.........
[/HIGH]


And this is where I try to use them in the CreateQuizActivity
[HIGH]
public class CreateQuizActivity extends Activity {

public int numberOfQuestions = 1;
public int questionNumber = 1;

protected TextView questionNumberLabel;
protected EditText question;
protected EditText choiceA;
protected EditText choiceB;
protected EditText choiceC;
protected EditText choiceD;
protected CheckBox choiceACheckBox;
protected CheckBox choiceBCheckBox;
protected CheckBox choiceCCheckBox;
protected CheckBox choiceDCheckBox;
protected Button nextButton;

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

numberOfQuestions = getNumberOfQuestions();

questionNumberLabel = (TextView) findViewById(R.id.questionNumberLabel);
question = (EditText) findViewById(R.id.question);
choiceA = (EditText) findViewById(R.id.choiceA);
choiceB = (EditText) findViewById(R.id.choiceB);
choiceC = (EditText) findViewById(R.id.choiceC);
choiceD = (EditText) findViewById(R.id.choiceD);
choiceACheckBox = (CheckBox) findViewById(R.id.choiceACheckBox);
choiceBCheckBox = (CheckBox) findViewById(R.id.choiceBCheckBox);
choiceCCheckBox = (CheckBox) findViewById(R.id.choiceCCheckBox);
choiceDCheckBox = (CheckBox) findViewById(R.id.choiceDCheckBox);
nextButton = (Button) findViewById(R.id.nextButton);

questionNumberLabel.setText("Question #" + questionNumber);

choiceACheckBox.setOnCheckedChangeListener(checkListener);
choiceBCheckBox.setOnCheckedChangeListener(checkListener);
choiceCCheckBox.setOnCheckedChangeListener(checkListener);
choiceDCheckBox.setOnCheckedChangeListener(checkListener);

nextButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if(question.getText().length() > 0 &&
choiceA.getText().length() > 0 &&
choiceB.getText().length() > 0 &&
choiceC.getText().length() > 0 &&
choiceD.getText().length() > 0 &&
isACheckBoxChecked()){

//get number of questions
numberOfQuestions = getNumberOfQuestions();
questionNumber = getQuestionNumber();

//check checkBoxes
boolean CheckA = choiceACheckBox.isChecked();
boolean CheckB = choiceBCheckBox.isChecked();
boolean CheckC = choiceCCheckBox.isChecked();
boolean CheckD = choiceDCheckBox.isChecked();

//check EditTexts
String A = choiceA.getText().toString();
String B = choiceB.getText().toString();
String C = choiceC.getText().toString();
String D = choiceD.getText().toString();

//Create Quiz Question Object
ParseObject quizQuestion = new ParseObject("QuizQuestion");
quizQuestion.put("question", question.getText().toString());
quizQuestion.put("choiceA", A);
quizQuestion.put("choiceB", B);
quizQuestion.put("choiceC", C);
quizQuestion.put("choiceD", D);
quizQuestion.put("choiceACheckBox", CheckA);
quizQuestion.put("choiceBCheckBox", CheckB);
quizQuestion.put("choiceCCheckBox", CheckC);
quizQuestion.put("choiceDCheckBox", CheckD);
quizQuestion.saveInBackground();

questionNumber++;

if(questionNumber < numberOfQuestions){
resetActivity();
}
else{
navigateToMain();
}
}
else{
//display warning Toast
Toast.makeText(CreateQuizActivity.this, R.string.quiz_not_correctly_filled_out_warning, Toast.LENGTH_LONG).show();
}

}
});


}
[/HIGH]

Here are the getters and setters in CreateQuizActivity
[HIGH]
public int getNumberOfQuestions() {
return numberOfQuestions;
}

public void setNumberOfQuestions(int numberOfQuestions) {
this.numberOfQuestions = numberOfQuestions;
}


public int getQuestionNumber() {
return questionNumber;
}

public void setQuestionNumber(int questionNumber) {
this.questionNumber = questionNumber;
}
[/HIGH]


The problem is HERE:

if(questionNumber < numberOfQuestions){
resetActivity();
}
else{
navigateToMain();
}

Although I think the numberOfQuestions is larger than questionNumber it always goes to navigateToMain() method. BTW resetActivity() and navigateToMain() work fine. My guess is that my variable placing is wrong somewhere.

Help is very appreciated :)
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones