我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
使用谷歌的Gson库,您可以将object传递给另一个活动。实际上,我们将以json字符串的形式转换对象,传递给其他活动后,我们将再次重新转换为这样的对象
考虑这样一个bean类
public class Example {
private int id;
private String name;
public Example(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我们需要传递Example类的对象
Example exampleObject=new Example(1,"hello");
String jsonString = new Gson().toJson(exampleObject);
Intent nextIntent=new Intent(this,NextActivity.class);
nextIntent.putExtra("example",jsonString );
startActivity(nextIntent);
对于读取,我们需要在NextActivity中做相反的操作
Example defObject=new Example(-1,null);
//default value to return when example is not available
String defValue= new Gson().toJson(defObject);
String jsonString=getIntent().getExtras().getString("example",defValue);
//passed example object
Example exampleObject=new Gson().fromJson(jsonString,Example .class);
在gradle中添加这个依赖
compile 'com.google.code.gson:gson:2.6.2'
其他回答
你可以使用android BUNDLE来做到这一点。
从你的类中创建一个Bundle,像这样:
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString("SomeKey", "SomeValue");
return b;
}
然后用INTENT传递这个bundle。 现在你可以通过传递bundle来重新创建你的类对象
public CustomClass(Context _context, Bundle b) {
context = _context;
classMember = b.getString("SomeKey");
}
在自定义类中声明并使用。
你的类应该实现Serializable或Parcelable。
public class MY_CLASS implements Serializable
一旦完成,你可以在putExtra上发送一个对象
intent.putExtra("KEY", MY_CLASS_instance);
startActivity(intent);
要得到额外的,你只需要做
Intent intent = getIntent();
MY_CLASS class = (MY_CLASS) intent.getExtras().getSerializable("KEY");
如果您的类实现了Parcelable,请使用下一个
MY_CLASS class = (MY_CLASS) intent.getExtras().getParcelable("KEY");
我希望它能帮到你
如果你有一个单例类(fx Service)作为你的模型层的网关,它可以通过在该类中有一个带有getter和setter的变量来解决。
活动一:
Intent intent = new Intent(getApplicationContext(), Activity2.class);
service.setSavedOrder(order);
startActivity(intent);
活动二:
private Service service;
private Order order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quality);
service = Service.getInstance();
order = service.getSavedOrder();
service.setSavedOrder(null) //If you don't want to save it for the entire session of the app.
}
在服务:
private static Service instance;
private Service()
{
//Constructor content
}
public static Service getInstance()
{
if(instance == null)
{
instance = new Service();
}
return instance;
}
private Order savedOrder;
public Order getSavedOrder()
{
return savedOrder;
}
public void setSavedOrder(Order order)
{
this.savedOrder = order;
}
此解决方案不需要对相关对象进行任何序列化或其他“打包”。但是,只有在使用这种架构时才会有好处。
我也遇到过同样的问题。我通过使用一个静态类来解决这个问题,在HashMap中存储我想要的任何数据。在上面,我使用了标准活动类的扩展,其中我覆盖了onCreate和onDestroy方法来做数据传输和数据清除隐藏。一些荒谬的设置必须改变,例如方向处理。
注释: 不提供一般对象传递给另一个活动是痛苦的屁股。这就像射击自己的膝盖,希望赢得100米。“比喻”并不是一个充分的替代品。这让我笑了……我不想实现这个接口到我的技术自由API,因为我更不想引入一个新的层…这是怎么回事,我们在移动编程中离现代范式如此之远……
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
startACtivity(i);
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。