简体   繁体   中英

Send to Intent an ArrayList<ArrayList<String>>

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM