首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Intent传递ArrayList

通过Intent传递ArrayList
EN

Stack Overflow用户
提问于 2011-03-21 14:38:04
回答 4查看 127.1K关注 0票数 84

我正在尝试使用意图将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");
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-21 14:47:04

在你的接收意图中,你需要做:

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

以你拥有它的方式,你只是在没有任何额外的东西的情况下创建了一个新的空意图。

如果你只有一个额外的东西,你可以把它压缩成:

stock_list = getIntent().getStringArrayListExtra("stock_list");
票数 108
EN

Stack Overflow用户

发布于 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");
票数 5
EN

Stack Overflow用户

发布于 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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5374546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档