Android中使用putExtra()传递对象实例
来源:互联网 发布:一级建造师 网络教育 编辑:程序博客网 时间:2024/06/06 19:42
在Android中,使用intent的putExtra()方法是没有办法传递实例的,但是我们很多情况下都需要传递实例过去,这样子会更加方便一点。接下里,我就说一下怎么使用putExtra()方法来进行实例的传递。
首先创建一个类:
package com.zkteco.petsystem;import java.io.Serializable;/** * Created by Administrator on 2017/9/5 0005. */public class ItemInfo implements Serializable{ private String name; private int attack; private int life; private int speed; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getLife() { return life; } public void setLife(int life) { this.life = life; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public ItemInfo(String name, int attack, int life, int speed) { this.name = name; this.attack = attack; this.life = life; this.speed = speed; } public ItemInfo(){ }}
一定要注意,
这个类要实现Serializable接口
,这是很关键的一步。
然后在传递的时候,使用putExtra()方法进行传递,详情见以下代码
Intent intent = new Intent(); intent.putExtra("itemInfo",itemInfo); setResult(1,intent); finish();
finish()方法用于结束销毁掉当前activity。
在接受数据的页面,使用getSerializableExtra()方法获取传递的值,要注意类型的强制转换。
ItemInfo itemInfo= (ItemInfo) data.getSerializableExtra("itemInfo");
阅读全文
0 0
- Android中使用putExtra()传递对象实例
- Android 中Intent的putExtra()方法的值传递
- Android 使用intent.putExtra实现Activity之间的参数传递
- Android 使用intent.putExtra实现Activity之间的参数传递
- 使用putExtra传递Serializable数据
- Android通过putExtra()传递JavaBean对象,属性值为null问题的解决
- Android 系列 4.4使用Intent.putExtra()推送字符串值
- Android中使用Intent传递对象
- Intent.putExtra()传递Object对象或者ArrayList<Object>
- Intent 传递中 Bundle与intent.putExtra 的关系
- Intent 传递中 Bundle与intent.putExtra 的关系
- 使用putExtra()在Activity和子Activity之间传递数据
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- JAVA中的length属性与length()方法
- 关于文件上传跨域及接收
- HDU6183 cdq分治+线段树
- Codeforces186C
- 闲来蛋疼写个qq侧滑删除
- Android中使用putExtra()传递对象实例
- bzoj 1003 [ZJOI2006]物流运输(最短路spfa+dp)
- 排序算法--冒泡排序
- Spiral Matrix
- 170903 WarGames-Behemoth(0)
- 内存优化总结:ptmalloc、tcmalloc和jemalloc
- 解决float属性导致换行
- 等错误率EER
- Firefox 2015 最受国人欢迎的十大扩展