代码改变世界

【原】android 中通过 aidl 传递map对象

  雪夜&流星  阅读(3086)  评论(0编辑  收藏  举报

 最近在做项目的过程中遇到了一个问题,我想通过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:

复制代码
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");
}
}
});
}

}
复制代码

  

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示