Passing ArrayList through Intent

locoboy picture locoboy · Mar 21, 2011 · Viewed 119k times · Source

I am trying to pass an arrayList to another activity using intents. Here is the code in the first activity.

case R.id.editButton:
        Toast.makeText(this, "edit was clicked", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, editList.class);
        intent.putStringArrayListExtra("stock_list", stock_list);
        startActivity(intent);
        break;

This is where I try to retrieve the list in the second activity. Is something wrong here?

Intent i = new Intent(); //This should be getIntent();
    stock_list = new ArrayList<String>();

    stock_list = i.getStringArrayListExtra("stock_list");

Answer

Philio picture Philio · Mar 21, 2011

In your receiving intent you need to do:

Intent i = getIntent();  
stock_list = i.getStringArrayListExtra("stock_list");

The way you have it you've just created a new empty intent without any extras.

If you only have a single extra you can condense this down to:

stock_list = getIntent().getStringArrayListExtra("stock_list");