Android Intent传递对象小结
效果:
想看实例的,感兴趣的能够看之前的一篇文章
Android ViewPager Fragment实现选项卡
部分关键代码:
public class SerializeActivity extends Activity implements Serializable {
Button btnlist, btnParcelable, btnSerialze;
private final String TAG = "SerializeActivity";
public final static String LIST_KEY = "ArrayList";
public final static String PAR_KEY = "parcelable";
public final static String SER_KEY = "serializable";
private static final long serialVersionUID = 1L;
private ArrayList<String> m_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_serialize);
initView();
initlist();
}
/*
* 初始化视图
*/
public void initView() {
btnlist = (Button) findViewById(R.id.btnlist);
btnParcelable = (Button) findViewById(R.id.btnParcelable);
btnSerialze = (Button) findViewById(R.id.btnSerialze);
btnlist.setOnClickListener(btnlistener);
btnParcelable.setOnClickListener(btnlistener);
btnSerialze.setOnClickListener(btnlistener);
}
public void initlist() {
m_list = new ArrayList<String>();
m_list.add("www.88ios.com");
m_list.add("Android移动开发");
m_list.add("IOS开发入门");
}
/*
* button点击事件
*/
OnClickListener btnlistener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlist:
intentList();
break;
case R.id.btnParcelable:
intentParcelabe();
break;
case R.id.btnSerialze:
intentShowSer();
break;
}
}
};
/*
*
*/
public void intentList() {
Intent list_intent = new Intent();
list_intent.putStringArrayListExtra(LIST_KEY, m_list);
list_intent.setClass(SerializeActivity.this, ShowInfo.class);
startActivity(list_intent);
}
/*
*
*/
public void intentParcelabe() {
Student m_Student = new Student();
m_Student.setName("88ios.com");
m_Student.setAge(25);
m_Student.setSex("男");
Intent p_Intent = new Intent(SerializeActivity.this,
ShowParcelabe.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(PAR_KEY, m_Student);
p_Intent.putExtras(mBundle);
startActivity(p_Intent);
}
/*
*
*/
public void intentShowSer() {
final SerializableMap myMap = new SerializableMap();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> maplist;
Map<String, Object> paramMap4 = new HashMap<String, Object>();
paramMap4.put("name", "洪生鹏");
paramMap4.put("website", "www.88ios.com");
paramMap4.put("address", "广州");
myMap.setMap(paramMap4);
SerializableMap serializableMap = (SerializableMap) myMap;
maplist = serializableMap.getMap();
list.add(maplist);
Intent mIntent = new Intent(SerializeActivity.this, ShowSer.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("map", (Serializable) myMap);
//mBundle.putSerializable("map", (Serializable) list);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
}
ShowInfo.java
public class ShowInfo extends Activity {
private Intent list_intent;
private ArrayList<String> m_arrayList;
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_info);
textview = (TextView) findViewById(R.id.textview);
StringBuffer sb = new StringBuffer();
list_intent = getIntent();
m_arrayList = list_intent.getExtras().getStringArrayList(
SerializeActivity.LIST_KEY);
m_arrayList.get(0);
sb.append(m_arrayList.get(0) + " \n" + m_arrayList.get(1) + "\n"
+ m_arrayList.get(2));
textview.setText(sb);
}
}
ShowParcelabe.java
public class ShowParcelabe extends Activity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_parcelabe);
textview = (TextView)findViewById(R.id.textview);
Student p_student = (Student)getIntent().getParcelableExtra(SerializeActivity.PAR_KEY);
textview.setText("姓名: " + p_student.getName()+"\n"+
"年龄: " + p_student.getAge() + "\n" +
"性别 : " + p_student.getSex() + "\n" +
"类:" + p_student.getClass());
}
}
ShowSer.java
public class ShowSer extends Activity {
private TextView textview;
StringBuilder sb = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_ser);
textview = (TextView)findViewById(R.id.textview);
Map<String, Object> maplist;
Bundle bundle = getIntent().getExtras();
SerializableMap serializableMap = (SerializableMap) bundle.get("map");
maplist = serializableMap.getMap();
/*
for (String k : maplist.keySet()) {
Toast.makeText(this, "" + maplist.get(k), Toast.LENGTH_SHORT)
.show();
}
*/
Set set = maplist.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
String valu = (String) entry.getValue();
sb.append(key+" "+valu+"\n");
}
textview.setText(sb);
}
}
转载请保留链接
http://hongshengpeng.com/article/show/271.aspx
交流群:154950206 进群验证:88ios
Android Intent传递对象小结的更多相关文章
- Android Intent传递对象摘要
效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ3NoZW5ncGVuZw==/font/5a6L5L2T/fontsize/400/fil ...
- Android 全局获取 Context 与使用 Intent 传递对象
=====================全局获取 Context======================== Android 开发中很多地方需要用到 Context,比如弹出 Toast.启动活 ...
- Android开发——使用intent传递对象
intent传递对象有两种方法: 方式一:Serializable 方式 方式二:Parcelable方式 在这里不多介绍了,这一篇就是快速上手使用教程,至于详细原理介绍的,请看这一篇http://w ...
- android#使用Intent传递对象
参考自<第一行代码>——郭霖 Intent的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent中添加一些附加数据,以达 ...
- Intent传递对象——Serializable和Parcelable差别
前两篇文章讨论了Serializable和Parcelable实现Intent之间传递对象和对象数组的方式.两种方法实现上相似,效果一致,怎么选择用哪种方法实现呢? Intent在不同的组件中传递对象 ...
- Intent传递对象的几种方式
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...
- 怎样使用Intent传递对象
怎样使用Intent传递对象 我们能够使用Intent来启动Activity.开启服务Service,发送广播Broadcast,然后使用Intent传递主要的数据类型,如:布尔值,整型,字符串等 I ...
- 关于intent传递对象后是传递的对象的地址还是对象的拷贝?
var intent = Intent(activity,SingleColorControlActivity::class.java); var bundle = Bundle()// bundle ...
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
随机推荐
- Android三种菜单简介
Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Menu).子菜单(Sub Menu). 一.选项菜单 用户点击设备上的菜单按钮(Menu),触发事件 ...
- Xcode 使用自定义字体
添加对应的字体(.ttf或.odf)到工程的resurce,使用cocos2d中的FontLabel库,FontLabel继承于UILabel,象UILabel一样使用就好了 fontName直接使用 ...
- filter过滤器执行顺序
浏览器请求---->进入过滤器---->进入doFilter方法--->执行chain.doFilter()方法就会放行----->进入业务逻辑方法------>进入过滤 ...
- linux常用命令(查看某些软件是否已安装)
查看imap是否已安装 rpm -qa | grep imap 以下为未安装的情形: 检查是否已安装sendmail: rpm -qa | grep sendmail 以下为已安装的返回:
- 执行*.sh脚本时提示Permission denied
使用chmod修改.sh的权限 chmod u+x *.sh 再次执行
- 一个简单的多线程Python爬虫(一)
一个简单的多线程Python爬虫 最近想要抓取拉勾网的数据,最开始是使用Scrapy的,但是遇到了下面两个问题: 前端页面是用JS模板引擎生成的 接口主要是用POST提交参数的 目前不会处理使用JS模 ...
- HDU2602 (0-1背包问题)
N - 01背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descri ...
- android 学习第一步
今天是2015年7月24号,今年下半年的主要学习方向是android,学习的目标是做出3个或以上的有实用价值的app.
- 应用SecureCRT(发送接收文件)
使用 SecureCRT 和 cz. sz,可以从 Linux 服务器上下载/上传文件. Linux 上要安装 lszrz 包 (1)编译安装root 账号登陆后,依次执行以下命令 cd /tmp w ...
- 工作总结:检查字符串合法性(C++)
BOOL CLiftCtrlModbusConfigDlg::CheckValid(const CString &str) { ASSERT(str.GetLength() > ); ] ...