0

In the activity A, I have built an array q1 of Question and I passed to activity B:

Intent i = new Intent(getApplicationContext(), B.class);
i.putExtra("questions", q1);
startActivity(i);
finish();

In the activity B:

Object c= getIntent().getExtras().getSerializable("questions");

Now, how can I reconvert "c" in an array of Questions? I can not make a cast (Question[])

5
  • Is Question marked as Serializable? Commented Jul 31, 2013 at 10:15
  • Yes the Question marked as Serializable. Commented Jul 31, 2013 at 10:17
  • Then you should be able to cast it. Are you getting an Exception? If so, can you post the stack trace/logcat entries. Commented Jul 31, 2013 at 10:18
  • 1
    Question[] is not a generic array. If you really mean Question[], then please change the title. Commented Jul 31, 2013 at 10:21
  • This code: codeQuestion[] q= (Question[]) getIntent().getExtras().getSerializable("questions"); Generates: 07-31 12:25:00.657: E/AndroidRuntime(25735): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.studentteech/com.example.studentteech.Domanda}: java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.example.studentteech.Question[] Commented Jul 31, 2013 at 10:25

4 Answers 4

2

this will be helpful.

 Question[] questions = (Question)context.getIntent().getExtras().get(key)
Sign up to request clarification or add additional context in comments.

Comments

0

Your objects need to implement the Parcelable interface.

In your Question class must implement parcelable interface. See the following code,

Question[] questions = //assign value;
Parcelable[] output = new Parcelable[questions.length];
for (int i=questions.length-1; i>=0; --i) {
    output[i] = questions[i];
}

Intent i = new Intent(...);
i.putExtra("QuestionArray", output);

//retreiving it from intent

Question[] questions = (Question[])intent.getParcelableArrayExtra("QuestionArray");

Comments

0

In your first activity you can send the array inside a bundle and then in the next activity that was created with the intent you can extract the array list from the bundle.

        Bundle b=new Bundle();
        b.putStringArrayList("option", optionMod);
        Intent i = new Intent(getApplicationContext(), ad45.hw.hwapp.confirmation.class);
        i.putExtras(b);
        startActivity(i); 

Then when you want to read the content in your new activity, create a new array List to store the one that was sent with the intent and extract the array from the bundle:

    public void getInfo(){
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
       options = extras.getStringArrayList("option"); 
     }
    }

Comments

0

Your Question object must implement the Parcelable interface. You can put only primitive objects or objects that implement Parcelable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.