1

I have in an activity an ArrayList<ArrayList<String>>, so basically a list of lists.

What I would like to accomplish, is to send from Activity 1 to Activity 2 this List of Lists, so I thought about intents, but I don't seem to be able to find the necessary getExtra().

To be more precise, questions contains Strings related to a question, and the corresponding position in answers contains all the selected answers to that question.

public class ReviewActivity extends AppCompatActivity {

ArrayList<String> questions = new ArrayList<>();
ArrayList<ArrayList<String>> answers = new ArrayList<>();

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

    Intent intent = getIntent();
    questions = intent.getStringArrayListExtra("questions");
    answers = intent.getStringArrayListExtra("answers");

}

This is how I put the original ArrayList<ArrayList<String>> in the intent:

 Intent mIntent = new Intent(this, ReviewActivity.class);
                            mIntent.putExtra("questions", questions);
                            mIntent.putExtra("answers", answers);
                            startActivity(mIntent);

The code gives an error at the line answers = intent.getStringArrayListExtra("answers");and I understand why, but how can I make a workaround?

Thanks.

Vanhaeren Thomas
  • 367
  • 4
  • 15

1 Answers1

3

ArrayList is Serializable so you can send it to bundle as Serializable.

 mIntent.putExtra("answers", answers);

To Get List :

ArrayList<ArrayList<String>> list= (ArrayList<ArrayList<String>>) getIntent().getExtras().getSerializable("answers");
ADM
  • 20,406
  • 11
  • 52
  • 83