[Q] Comparing Intent results

Put your problem here if it does not fit any of the other categories.

[Q] Comparing Intent results

Postby FakeCaker » Thu Mar 24, 2011 4:12 pm

hi,
i'm trying out intents at the moment, and it went pretty well, untill now.
i've got a little quiz-like app: First you see a main screen - press button to start new activity - answer Question -
- return to mainscreen and display answer you've selected, and wether it's good or wrong...

Now i got it to set a textview to the selected answer, but my good/wrong line keeps saying it's wrong...


mainscreen class :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.quiz.test;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. public class mainActivity extends Activity {
  11.  
  12.         @Override
  13.         public void onCreate(Bundle savedInstanceState) {
  14.                 super.onCreate(savedInstanceState);
  15.                 setContentView(R.layout.main);
  16.  
  17.                 TextView tvAnswer = (TextView) findViewById(R.id.tv_answer);
  18.                 TextView tvTrueFalse = (TextView) findViewById(R.id.tv_truefalse);
  19.                 tvAnswer.setText("Nothing yet,");
  20.                 tvTrueFalse.setText("just fine");
  21.  
  22.         }
  23.  
  24.         public void buttonHandler(View v) {
  25.                 Intent questionIntent = new Intent(getApplicationContext(),
  26.                                 com.quiz.test.QuestionActivity.class);
  27.                 startActivityForResult(questionIntent, 0);
  28.  
  29.         }
  30.  
  31.         @Override
  32.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  33.         TextView tvAnswer = (TextView)findViewById(R.id.tv_answer);
  34.         TextView tvTrueFalse = (TextView)findViewById(R.id.tv_truefalse);
  35.        
  36.         tvAnswer.setText(data.getExtras().getString("givenAnswer"));
  37.        
  38.        
  39.                        
  40.         if(data.getExtras().getString("givenAnswer")
  41.                         ==data.getExtras().getString("correctAnswer")){
  42.                 tvTrueFalse.setText("true !");
  43.                 Toast.makeText(this, data.getExtras().getString("correctAnswer"),
  44.                                 Toast.LENGTH_SHORT);
  45.         }
  46.                
  47.     else tvTrueFalse.setText("false");
  48.                 }
  49.        
  50. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Question screen class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.quiz.test;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7.  
  8. public class QuestionActivity extends Activity {
  9.        
  10.         @Override
  11.         protected void onCreate(Bundle savedInstanceState) {
  12.                 setContentView(R.layout.question);
  13.                 super.onCreate(savedInstanceState);
  14.  
  15.                 }
  16.                
  17.         public void radioButtonHandler(View v){
  18.                
  19.                 Intent answer = new Intent();
  20.                
  21.                 switch(v.getId()){
  22.                
  23.                 case R.id.rb_blue: answer.putExtra("givenAnswer", "blue");break;
  24.                 case R.id.rb_purple: answer.putExtra("givenAnswer", "purple");break;
  25.                 case R.id.rb_red: answer.putExtra("givenAnswer", "red");break;
  26.                 case R.id.rb_yellow: answer.putExtra("givenAnswer", "yellow");break;   
  27.                 }
  28.                 answer.putExtra("correctAnswer", "red");
  29.                
  30.                 setResult(0, answer);
  31.                 finish();              
  32.         }
  33.        
  34.                        
  35.         }
  36.  
  37.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


If need for more explaination/code commenting, feel free to ask.
Thanks :mrgreen:
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Top

Re: [Q] Comparing Intent results

Postby raj_J2ME » Fri Mar 25, 2011 8:43 am

Where are you getting the error in the code and what is the exact error please let us know the details about the same?
is the error is coming at the time when you are running the app or is it the error inside the IDE?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: [Q] Comparing Intent results

Postby FakeCaker » Fri Mar 25, 2011 1:42 pm

it's not really an error, but it just doesn't work like it should:
device.png
device.png (10.06 KiB) Viewed 129 times
device2.png
device2.png (8.54 KiB) Viewed 129 times


In my intent i also added "correctAnswer" wich has the value of "red".
so back in my mainActivity, i used IF statement to check wether "givenAnswer" == "correctAnswer".
if it's true, set TextView to "true !". ELSE set it to "false".
but no matter which answer i choose, it keeps saying false. (see code for more info ^^)

i'm i clear enough now ? :D
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 31 guests