问题将arraylist从意图传递到另一个意图 [英] issue to pass arraylist from an intent to another

查看:169
本文介绍了问题将arraylist从意图传递到另一个意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在问这里之前,我首先搜索如何从一个意图传递一个阵列列表到另一个意图。感谢SO上发布的一篇文章,我认为我找到了一种方法。



事情是,当我尝试显示来自数组列表的元素时我的目标活动,我只得到我原来在离职活动中的众多元素之一。



为了说清楚,我的代码在我的fisrt活动中:

  HashMap map = new HashMap< String,String>(); 

map.put(key1,value1);
map.put(key2,value2);

arrayList.add(map);
Log.d(arrayList,String.valueOf(arrayList));

在logcat中,我有预期的元素:

  D / arrayList :: [{key1:value1},{key2,value2} 

然后,我追求这一点:

 意图意图= new Intent(MyFirstActivity.this,MySecondActivity.class); 
intent.putExtra(arrayList,arrayList);

现在,这是我的第二个活动,应该是收到的元素。

  Intent intent = getIntent(); 
arrayList =(ArrayList< HashMap< String,String>>)getIntent()。getSerializableExtra(arrayList);
Log.d(arraySecondActivity,String.valueOf(arrayList));

在logcat中,只显示我的第二个项目:

  D / arraySecondActivity:[{key2 = value2}] 

我不知道为什么......有人可能知道吗?



谢谢!

解决方案

intent.putExtra(arrayList,arrayList); 在代码中存在这个错误。
序列化Array列表之前像下面传递ArrayList

尝试此代码

  ArrayList< Object> object = new ArrayList< Object>(); 
Intent intent = new Intent(Current.class,next.class);
Bundle args = new Bundle();
args.putSerializable(ARRAYLIST,(Serializable)object);
intent.putExtra(BUNDLE,args);
startActivity(intent);

在接下来的类中

  Intent intent = getIntent(); 
Bundle args = intent.getBundleExtra(BUNDLE);
ArrayList< Object> object =(ArrayList< Object>)args.getSerializable(ARRAYLIST);


Before asking here, i first search how to pass an arraylist from an intent to another. Thanks to one of the post made on SO, I thought i found a way to do so.

The thing is, when i try to display the elements from the arraylist of my target activity, i only get one of the numerous elements i originally have in my departure activity.

To put it in clear, here is my code in my fisrt activity :

HashMap map = new HashMap<String,String>();

map.put("key1","value1");
map.put("key2","value2");

arrayList.add(map);
Log.d("arrayList", String.valueOf(arrayList));

In the logcat, I have the expected elements :

D/arrayList:: [{"key1":"value1"},{"key2","value2"}

Then, i pursue with this :

Intent intent = new Intent(MyFirstActivity.this,MySecondActivity.class);
intent.putExtra("arrayList",arrayList);

Now, this is my second activity where should be the received elements.

Intent intent = getIntent();
arrayList = (ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("arrayList");
Log.d("arraySecondActivity", String.valueOf(arrayList));

In the logcat, only my second item is display :

D/arraySecondActivity: [{key2=value2}]

and i don't know why ... does someone maybe know ?

thanks !

解决方案

intent.putExtra("arrayList",arrayList); this wrong in your code. Serializable the arraylist before pass like below

Try this code

 ArrayList<Object> object = new ArrayList<Object>();
 Intent intent = new Intent(Current.class, next.class);
 Bundle args = new Bundle();
 args.putSerializable("ARRAYLIST",(Serializable)object);
 intent.putExtra("BUNDLE",args);
 startActivity(intent);

In the next.class

 Intent intent = getIntent();
 Bundle args = intent.getBundleExtra("BUNDLE");
 ArrayList<Object> object = (ArrayList<Object>)  args.getSerializable("ARRAYLIST");

这篇关于问题将arraylist从意图传递到另一个意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆