如何将List <string>作为Intent extra传递?

时间:2017-01-12 10:58:27

标签: java android arrays string parse-platform

我有一个List,我想将其转换为String [],以便我可以将其作为额外的意图传递给它。

List<String> votedBy = parseObject.getList("votedBy");

意图逻辑:

Intent intent = new Intent(CommentActivity.this, UsersListActivity.class);
intent.putExtra(BundleConstants.KEY_LIST_TYPE, getString(R.string.voters));
intent.putExtra(BundleConstants.KEY_VOTERS_LIST, votedBy);
startActivity(intent);

我的第一个想法是将List的内容转换为String [],但是该列表是从数据库中检索的,并且其大小未知是先验的。我提出的一个建议是将votedBy List列为Parcelable。我目前无法运行代码,因此我不确定这是否有效。我该怎么办?

3 个答案:

答案 0 :(得分:7)

这用于使用一个活动的意图发送。

            Intent intent = getIntent();
            intent.putStringArrayListExtra("votedBy",new ArrayList<>(votedBy));

要接受这个可以使用;

ArrayList<String> votedByList= getIntent().getStringArrayListExtra("votedBy");

答案 1 :(得分:1)

据我所知,List不能作为Intent extra传递。

我建议传递从数据库中检索到的原始字符串,然后直接在新活动(UserListActivity)上解析它。

答案 2 :(得分:1)

我认为你应该试试

List<String> list = ..;
String[] array = list.toArray(new String[0]);

this question中所述 如您所见,列表的大小永远不会成为转换的问题,您永远不需要知道它。

相关问题