我正在尝试使用意图将arrayList传递给另一个活动。以下是第一个活动中的代码。
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;
这是我尝试在第二个活动中检索列表的地方。这里有什么问题吗?
Intent i = new Intent(); //This should be getIntent();
stock_list = new ArrayList<String>();
stock_list = i.getStringArrayListExtra("stock_list");
发布于 2011-03-21 14:47:04
在你的接收意图中,你需要做:
Intent i = getIntent();
stock_list = i.getStringArrayListExtra("stock_list");
以你拥有它的方式,你只是在没有任何额外的东西的情况下创建了一个新的空意图。
如果你只有一个额外的东西,你可以把它压缩成:
stock_list = getIntent().getStringArrayListExtra("stock_list");
发布于 2018-05-29 14:18:42
假设您需要将以下类的数组列表从当前活动传递到下一个活动//数组列表中的对象的类//记住从Serializable接口实现类// Serializable意味着它将对象转换为字节流并帮助传输该对象
public class Question implements Serializable {
...
...
...
}
在您当前的活动中,您可能有一个ArrayList,如下所示
ArrayList<Question> qsList = new ArrayList<>();
qsList.add(new Question(1));
qsList.add(new Question(2));
qsList.add(new Question(3));
// intialize Bundle instance
Bundle b = new Bundle();
// putting questions list into the bundle .. as key value pair.
// so you can retrieve the arrayList with this key
b.putSerializable("questions", (Serializable) qsList);
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);
为了在下一个活动中获得数组列表
//get the bundle
Bundle b = getIntent().getExtras();
//getting the arraylist from the key
ArrayList<Question> q = (ArrayList<Question>) b.getSerializable("questions");
发布于 2018-08-29 20:28:02
//arraylist/Pojo you can Pass using bundle like this
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle args = new Bundle();
args.putSerializable("imageSliders",(Serializable)allStoriesPojo.getImageSliderPojos());
intent.putExtra("BUNDLE",args);
startActivity(intent);
Get SecondActivity like this
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
String filter = bundle.getString("imageSliders");
//Happy coding
https://stackoverflow.com/questions/5374546
复制相似问题