0

I am sending data from one activity to another through intent. I am sending two different strings but getting same value for both variable on next activity.

Here is my code :

public class Quizzes extends ActionBarActivity {

    protected static final String QUIZ_TITLE = null;
    protected static final String COURSE = null;

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

        listView = (ListView) findViewById(R.id.listview);


        String[] values = new String[] { "Quiz # 2" };


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, values);



        listView.setAdapter(adapter); 

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                final String item = (String) parent.getItemAtPosition(position);

                Intent intent = new Intent(getApplicationContext(), QuizDetail.class);
                intent.putExtra(QUIZ_TITLE, item);  
                final String course = (String)textview.getText();
                intent.putExtra(COURSE, course);

                startActivity(intent);

            }

          });
        }

}

If you see i am passing two string intent object : 1. QUIZ_TITLE 2. COURSE

When i debugged the application, I can see values like 1. QUIZ_TITLE = "Quiz # 1" 2. COURSE = "Intro to Computing"

All fine until here, but when i am retrieving these string on other activity, I am getting value "Intro to Computing" for both, here is code from that class.

public class QuizDetail extends ActionBarActivity {

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

        Intent intent = getIntent();

        String quizTitle = intent.getStringExtra(Quizzes.QUIZ_TITLE);
        TextView quizTitleTextView = (TextView) findViewById(R.id.quizTitle);
        quizTitleTextView.setText(quizTitle+" : TESTING..");

        String courseTitle = intent.getStringExtra(Quizzes.COURSE);
        TextView courseTitleTextView = (TextView) findViewById(R.id.courseTitle);
        courseTitleTextView.setText(courseTitle);

    }
} 

I am not sure why I am getting same value "Intro to computing" from Quizzes.QUIZ_TITLE and Quizzes.COURSE. Any help would be highly appreciated.

Thanks.. Anjum

Anjum
  • 681
  • 3
  • 14
  • 38

3 Answers3

1

You are using bad the intent.putExtra(),

You need to put a key (you need to know) as first param, to get the object in the other activity like:

...

String item = ...;

intent.putExtra("COURSE", item);

...

And you get the extras with:

...

intent.getStringExtra("COURSE");

...

Edited !!!

Anjum
  • 681
  • 3
  • 14
  • 38
Arturo
  • 261
  • 1
  • 4
  • 19
  • Thanks bro but have a look at here :http://developer.android.com/training/basics/firstapp/starting-activity.html – Anjum Dec 04 '14 at 09:33
  • Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); – Anjum Dec 04 '14 at 09:33
  • You'll define the EXTRA_MESSAGE constant in a moment. – Anjum Dec 04 '14 at 09:33
1

There's a couple of things here that should be mentioned.

QUIZ_TITLE and COURSE are both null (I can't see where they're set) When you add something to the Extras Bundle, you're placing values in to a dictionary. The key for this dictionary you're using, in this case, is null. This means the second time you're putting in to the dictionary, QUIZ_TITLE (null) is being replaced with the key COURSE (null).

If you change QUIZ_TITLE and COURSE to an actual String value, it should sort that problem.

The second thing to note, is that there's a difference between getExtraString and getExtras.getString. I have written about this here

Hope that helps.

Community
  • 1
  • 1
Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
0

Please try this

Intent intent = getIntent();

String quizTitle = intent.getExtras().getString(Quizzes.QUIZ_TITLE);
String courseTitle = iintent.getExtras().getString(Quizzes.COURSE);

Update:

Oh now i see it too:

protected static final String QUIZ_TITLE = null;
protected static final String COURSE = null;

is really fatal because using a null value for a key is not useful and even if it is possible you are setting your value for the key 'null' first and overwrite it then by setting the value for key 'null' again.

Change it to:

protected static final String QUIZ_TITLE = "extra_quiz_title"
protected static final String COURSE = "extra_course";

for example

maimArt
  • 389
  • 1
  • 11