如何在 Android 上将整个对象传递给另一个 activity
how to pass a entire object to another activity on Android
我正在尝试将整个对象从一个 activity 传递和接收到另一个,为此,我使用 Bundle 将使用不同 "key" 分隔的每个属性值放入对象的每个属性,但对象太大而无法将属性传递给属性。我想知道是否有一种方法可以传递整个对象而不是将属性传递给属性。
是否有任何方法可以在 class 上创建一个构造函数来接收对象?所以当这个 class 被调用时,它必须接收对象
Bundle valores;
valores.putInt("CODIGO_CHECK", check.getCod());
valores.putInt("CODIGO_MODELO", check.getCodModeloCheckList());
valores.putInt("COD_VEICULO_W", check.getCodVeiculoW());
valores.putString("PLACA", check.getPlacaVeiculo());
valores.putString("DATAINICIO", check.getDataInicioVistoria());
valores.putString("CAMINHO_FT_PLACA", check.getCaminhoArquivoFotoPlacaVeiculo());
valores.putString("COMENTARIO", check.getComentarios());
valores.putString("MANIFESTO", check.getNumeroManifesto());
valores.putString("SISTEMAS", check.getIdentificadorSistemasTerceiros());
valores.putString("CONHECIMENTO", check.getNumeroConhecimentoTransporte());
valores.putBoolean("SOMENTERBQ", check.getFlgSomenteReboques());
valores.putInt("STATUS_CHECK", check.getStatusCheckList());
valores.putString("MODELO", check.getNomeCheck());
valores.putInt("BLOCKBUTTON", 1);
Intent intent = new Intent(getApplicationContext(), SavedCheckActivity.class);
intent.putExtras(valores);
startActivity(intent);
这实际上是我传递数据的方式..问题是这个 class(check) 有更多的属性..发送和接收这些数据变得越来越困难
您需要使用 Parcelable
接口将对象从一个 activity 传递给另一个。
您要传递其 class 的对象应实现 Parcelalbe
接口,如下所示:
public class MyData implements Parcelable {
...
}
同时在class
里面添加如下方法
//write object values to parcel for storage
public void writeToParcel(Parcel dest, int flags){
//write all properties to the parcle
}
//constructor used for parcel
public MyData(Parcel parcel){
//read and set saved values from parcel
}
//creator - used when un-parceling our parcle (creating the object)
public static final Parcelable.Creator<Property> CREATOR = new Parcelable.Creator<MyData>(){
@Override
public MyData createFromParcel(Parcel parcel) {
return new MyData(parcel);
}
@Override
public MyData[] newArray(int size) {
return new MyData[0];
}
};
//return hashcode of object
public int describeContents() {
return hashCode();
}
然后就可以传数据了
Intent intent = new Intent(getBaseContext(), NextActivity.class);
MyData data = new MyData();
intent.putExtra("data", data);
startActivity(intent);
在目标 activity 中,您可以按如下方式检索对象:
MyData data = getIntent().getExtras().getParcelable("data");
您可以直接在 intent 中传递对象:
Intent intent = new Intent(getApplicationContext(), SavedCheckActivity.class);
intent.putExtra("keyObject", check);
startActivity(intent);
同时将 class 检查标记为可序列化为:
public class check implements Serializable
{
//Attribures & Methods
}
并且您可以将意图接收为:
Intent rcv = getIntent();
Check check = (Check) rcv.getSerializableExtra("keyObject");
我正在尝试将整个对象从一个 activity 传递和接收到另一个,为此,我使用 Bundle 将使用不同 "key" 分隔的每个属性值放入对象的每个属性,但对象太大而无法将属性传递给属性。我想知道是否有一种方法可以传递整个对象而不是将属性传递给属性。
是否有任何方法可以在 class 上创建一个构造函数来接收对象?所以当这个 class 被调用时,它必须接收对象
Bundle valores;
valores.putInt("CODIGO_CHECK", check.getCod());
valores.putInt("CODIGO_MODELO", check.getCodModeloCheckList());
valores.putInt("COD_VEICULO_W", check.getCodVeiculoW());
valores.putString("PLACA", check.getPlacaVeiculo());
valores.putString("DATAINICIO", check.getDataInicioVistoria());
valores.putString("CAMINHO_FT_PLACA", check.getCaminhoArquivoFotoPlacaVeiculo());
valores.putString("COMENTARIO", check.getComentarios());
valores.putString("MANIFESTO", check.getNumeroManifesto());
valores.putString("SISTEMAS", check.getIdentificadorSistemasTerceiros());
valores.putString("CONHECIMENTO", check.getNumeroConhecimentoTransporte());
valores.putBoolean("SOMENTERBQ", check.getFlgSomenteReboques());
valores.putInt("STATUS_CHECK", check.getStatusCheckList());
valores.putString("MODELO", check.getNomeCheck());
valores.putInt("BLOCKBUTTON", 1);
Intent intent = new Intent(getApplicationContext(), SavedCheckActivity.class);
intent.putExtras(valores);
startActivity(intent);
这实际上是我传递数据的方式..问题是这个 class(check) 有更多的属性..发送和接收这些数据变得越来越困难
您需要使用 Parcelable
接口将对象从一个 activity 传递给另一个。
您要传递其 class 的对象应实现 Parcelalbe
接口,如下所示:
public class MyData implements Parcelable {
...
}
同时在class
里面添加如下方法//write object values to parcel for storage
public void writeToParcel(Parcel dest, int flags){
//write all properties to the parcle
}
//constructor used for parcel
public MyData(Parcel parcel){
//read and set saved values from parcel
}
//creator - used when un-parceling our parcle (creating the object)
public static final Parcelable.Creator<Property> CREATOR = new Parcelable.Creator<MyData>(){
@Override
public MyData createFromParcel(Parcel parcel) {
return new MyData(parcel);
}
@Override
public MyData[] newArray(int size) {
return new MyData[0];
}
};
//return hashcode of object
public int describeContents() {
return hashCode();
}
然后就可以传数据了
Intent intent = new Intent(getBaseContext(), NextActivity.class);
MyData data = new MyData();
intent.putExtra("data", data);
startActivity(intent);
在目标 activity 中,您可以按如下方式检索对象:
MyData data = getIntent().getExtras().getParcelable("data");
您可以直接在 intent 中传递对象:
Intent intent = new Intent(getApplicationContext(), SavedCheckActivity.class);
intent.putExtra("keyObject", check);
startActivity(intent);
同时将 class 检查标记为可序列化为:
public class check implements Serializable
{
//Attribures & Methods
}
并且您可以将意图接收为:
Intent rcv = getIntent();
Check check = (Check) rcv.getSerializableExtra("keyObject");