通过 Intent 传递 ArrayList
问题描述:
我正在尝试使用意图将 arrayList 传递给另一个活动.这是第一个活动中的代码.
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");
答
在你的接收意图中你需要做的:
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");