【原】android 中通过 aidl 传递map对象
2011-07-31 19:32
1316 查看
最近在做项目的过程中遇到了一个问题,我想通过aidl传递一个intent对象过去,intent中传递的数据是一个map对象,而map中是以键值对保存的一个对象的数据集合。
我的想法是 即通过 aidl传递-------intent(包含一个bundle对象 而bundle中放的是Bundle.putSerializable(String
key, Serializable
value))--------Serializable value(这里我想放的是一个map对象)-----HashMap(Map<String, ArrayList<Entity>> String为这个数据集合的一个标记 可以通过这个标记获取这个数据集合)---- ArrayList<Entity> (实体的数据集合类)。
而我却卡在Serializable value,刚开始我以为我的实体Entity实现了序列化接口就可以传递了,经过实验发现不行。后来的解决办法是将map封装了一下,实现了序列化接口就OK了。
下面是我实验的demo(只用了两个Activity,一个用来传递数据,一个用来接受数据):
封装的map:
实体Entity:
用于传递数据的Activity:
用来接受数据的Activity:
我的想法是 即通过 aidl传递-------intent(包含一个bundle对象 而bundle中放的是Bundle.putSerializable(String
key, Serializable
value))--------Serializable value(这里我想放的是一个map对象)-----HashMap(Map<String, ArrayList<Entity>> String为这个数据集合的一个标记 可以通过这个标记获取这个数据集合)---- ArrayList<Entity> (实体的数据集合类)。
而我却卡在Serializable value,刚开始我以为我的实体Entity实现了序列化接口就可以传递了,经过实验发现不行。后来的解决办法是将map封装了一下,实现了序列化接口就OK了。
下面是我实验的demo(只用了两个Activity,一个用来传递数据,一个用来接受数据):
封装的map:
public class MyMap implements Serializable{ private Map<String, ArrayList<Entity>> map; public Map<String, ArrayList<Entity>> getMap() { return map; } public void setMap(Map<String, ArrayList<Entity>> map) { this.map = map; } }
实体Entity:
public class Entity implements Serializable{ String id; String name; String age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
用于传递数据的Activity:
public class MapDemoActivity extends Activity { Button button; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Map<String, ArrayList<Entity>> map=initData(); final MyMap myMap=new MyMap(); //将map数据添加到封装的map中 myMap.setMap(map); button=(Button) findViewById(R.id.btn); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MapDemoActivity.this,ReceiveMap.class); Bundle bundle=new Bundle(); bundle.putSerializable("map", myMap); intent.putExtras(bundle); startActivity(intent); } }); } /** * 封装数据 * @return map对象 */ private Map<String, ArrayList<Entity>> initData() { // TODO Auto-generated method stub Map<String, ArrayList<Entity>> map=new HashMap<String, ArrayList<Entity>>(); for(int i=0;i<10;i++){ ArrayList<Entity> list=new ArrayList<Entity>(); for(int j=0;j<10;j++){ Entity entity=new Entity(); entity.setId(""+j); entity.setName("name"+j); entity.setAge("age"+j); list.add(entity); } map.put("path"+i, list); } return map; } }
用来接受数据的Activity:
public class ReceiveMap extends Activity { TextView textView; Button button; MyMap myMap; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.receive_data); Bundle bundle=getIntent().getExtras(); myMap=(MyMap) bundle.get("map"); textView=(TextView) findViewById(R.id.tv); button=(Button) findViewById(R.id.receive_btn); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Map<String, ArrayList<Entity>> map=myMap.getMap(); if(map==null) return ; for(int i=0;i<10;i++){ ArrayList<Entity> list=map.get("path"+i); if(list==null||list.size()==0) continue; textView.append("map key--"+"path"+i+"---list size is---"+list.size()+"\n"); } } }); } }
相关文章推荐
- Android 通过 Intent 传递类对象
- Android 开发笔记——通过 Intent 传递类对象
- 在Android中通过Intent使用Bundle传递对象
- Android--Service之AIDL传递复杂对象
- AIDL简单使用(通过它跨应用传递对象(数据))
- Android:通过Intent在两个Activity之间传递对象(Parcelable)
- Android 如何通过Intent在activity之间传递自定义对象 推荐
- 在Android中通过Intent使用Bundle传递对象
- Android--Service之AIDL传递复杂对象
- Android 通过 Intent 传递类对象
- android通过ksoap访问webservice方法传递一个复杂对象参数
- 在Android中通过Intent使用Bundle传递对象
- Android基础 -- Activity之间传递数据(bitmap和map对象)
- Android 通过intent 传递对象
- 在Android中通过Intent使用Bundle传递对象
- Android 开发笔记——通过 Intent 传递类对象
- android 通过Intent使用Bundle传递对象
- Android ListView 点击item 监听事件,通过intent传递对象
- Android:通过Intent传递对象、Parcelable
- Android通过Parcelable传递复杂的对象数据