android intent 传递各种结构数据

android intent 传递list或者对象

方法一: 
如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 

Java代码 

intent.putStringArrayListExtra(name, value)  

intent.putIntegerArrayListExtra(name, value)  


方法二: 
如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过 
Java代码  putExtras(key, (Serializable)list)  
方法传递过去,接受的时候用 
Java代码  (List<YourObject>) getIntent().getSerializable(key)  
就可以接受到List<YourObject>数据了 

但是 切记 你的YourObject类必须要实现Serializable接口 

方法三: 
一种是 
Java代码  Bundle.putSerializable(Key,Object);  
另一种是 
Java代码  Bundle.putParcelable(Key, Object);  
当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口

一、传递List<String>和List<Integer>
以下以传递List<String>为例,发送List<String>语法为:
intent.putStringArrayListExtra(key, list);
接收List<String>的语法为:
list = (ArrayList<String>)getIntent().getStringArrayListExtra(key);
以下是一个运用实例:


ArrayList<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
stringList.add("string3");
Intent intent = new Intent();
intent.setClass(ListDemoActivity.this, StringListActivity.class);
intent.putStringArrayListExtra("ListString", stringList);
startActivity(intent);
接收
ArrayList<String> stringList = (ArrayList<String>) getIntent().getStringArrayListExtra("ListString");

List<Integer>类似以上的操作调用下面的方法也可以实现发送和接收:
intent.putIntegerArrayListExtra(key, list);
list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);

Intent 传递String 字符串

intent.putExtra("key", "hello, world!");

 

接收端写法:

Intent intent = getIntent();
intent.getStingExtra("key"),如果key对应的字符串没有找到则得到null;

猜你喜欢

转载自dasheny.iteye.com/blog/2170916