I have a quiz game of 5 questions that shows results as a rating bar that I also want to show on the start of the quiz. Here is my quiz activity where the score "int" is generated.
public class QuizActivityt1 extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DbHelpert1 db=new DbHelpert1(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score : "+score);
}
if(qid<5){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(QuizActivityt1.this, ResultActivityt1.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b);
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}
I was able to send it to send it via intent the result activity and then show it as a rating bar as the following.
public class ResultActivityt1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//get rating bar object
RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1);
bar.setNumStars(5);
bar.setStepSize(0.5f);
bar.setEnabled(false);
//get text view
TextView t=(TextView)findViewById(R.id.textResult);
//get score
Bundle b = getIntent().getExtras();
int score= b.getInt("score");
//display score
bar.setRating(score);
switch (score)
{
case 1:
case 2: t.setText("Oopsie! Better Luck Next Time!");
break;
case 3:
case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia");
break;
case 5:
case 6:t.setText("Who are you? A trivia wizard???");
break;
};
}
So far so good, but what I have attempting to do is to make that intent go also on my Main activity to show the score elsewhere, here is my main activity:
public class Tech extends Activity {
Button T1,T2,T3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tech);
RatingBar rbt1=(RatingBar)findViewById(R.id.rbt1);
rbt1.setNumStars(5);
rbt1.setStepSize(0.5f);
rbt1.setEnabled(false);;
RatingBar rbt2=(RatingBar)findViewById(R.id.rbt2);
rbt2.setEnabled(false);
RatingBar rbt3=(RatingBar)findViewById(R.id.rbt3);
rbt3.setEnabled(false);
T1 = (Button)findViewById(R.id.T1);
T2 = (Button)findViewById(R.id.T2);
T3 = (Button)findViewById(R.id.T3);
T1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(Tech.this, QuizActivityt1.class);
//Optional parameters
startActivity(myIntent);
}
});
T2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(Tech.this, QuizActivityt2.class);
//Optional parameters
startActivity(myIntent);
}
});
T3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(Tech.this, QuizActivityt3.class);
//Optional parameters
startActivity(myIntent);
}
});
}
I know this may seem like an easy question, but unfortunately I am a beginner and I tried many workarounds I found similar to my case.
0 comments:
Post a Comment