本问题已经有最佳答案,请猛点这里访问。
我需要通过intent将类对象传递给另一个活动。 这是我的班级代码:
public class Model
{
private String Name;
private ArrayList trim;
public String getName()
{
return Name;
}
public void setName(String Name)
{
this.Name = Name;
}
public ArrayList getTrim()
{
return trim;
}
public void setTrim(ArrayList trim)
{
this.trim = trim;
}
}
这段代码中有什么? 发布活动代码
可能重复:stackoverflow.com/q/2139134/760489
使用Parcelable或serializable实现。 实施例
要将对象传递给另一个活动,您需要实现Parcelable。
请仔细阅读为Android编写Parcelable类。 在这里,他们使用Hashmap存储值并将对象传递给另一个类。
要么
创建一个类ObjectA。 在那,我使用了所有的setter和getter方法。
package com.ParcableExample.org;
import android.os.Parcel;
import android.os.Parcelable;
/**
* A basic object that can be parcelled to
* transfer between objects.
*/
public class ObjectA implements Parcelable
{
private String strValue = null;
private int intValue = 0;
/**
* Standard basic constructor for non-parcel
* object creation.
*/
public ObjectA()
{
}
/**
*
* Constructor to use when re-constructing object
* from a parcel.
*
* @param in a parcel from which to read this object.
*/
public ObjectA(Parcel in)
{
readFromParcel(in);
}
/**
* Standard getter
*
* @return strValue
*/
public String getStrValue()
{
return this.strValue;
}
/**
* Standard setter
*
* @param strValue
*/
public void setStrValue(String strValue)
{
this.strValue = strValue;
}
/**
* Standard getter
*
* @return intValue
*/
public Integer getIntValue()
{
return this.intValue;
}
/**
* Standard setter
*
* @param strValue
*/
public void setIntValue(Integer intValue)
{
this.intValue = intValue;
}
@Override
public int describeContents()
{
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
// We just need to write each field into the
// parcel. When we read from parcel, they
// will come back in the same order
dest.writeString(this.strValue);
dest.writeInt(this.intValue);
}
/**
*
* Called from the constructor to create this
* object from a parcel.
*
* @param in parcel from which to re-create object.
*/
public void readFromParcel(Parcel in)
{
// We just need to read back each
// field in the order that it was
// written to the parcel
this.strValue = in.readString();
this.intValue = in.readInt();
}
/**
*
* This field is needed for Android to be able to
* create new objects, individually or as arrays.
*
* This also means that you can use use the default
* constructor to create the object and use another
* method to hyrdate it as necessary.
*/
@SuppressWarnings("unchecked")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
@Override
public ObjectA createFromParcel(Parcel in)
{
return new ObjectA(in);
}
@Override
public Object[] newArray(int size)
{
return new ObjectA[size];
}
};
}
然后创建一个用于将Object发送到另一个活动的Activity。
package com.ParcableExample.org;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ParcableExample extends Activity
{
private Button btnClick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
btnClick = (Button)findViewById(R.id.btnClick);
btnClick.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
ObjectA obj = new ObjectA();
obj.setIntValue(1);
obj.setStrValue("Chirag");
Intent i = new Intent(ParcableExample.this,MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);
}
});
}
}
现在最后再创建另一个读取Object的活动并从中获取值。
package com.ParcableExample.org;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = getIntent().getExtras();
ObjectA obj = bundle.getParcelable("com.package.ObjectA");
Log.i("---------- Id ","::"+obj.getIntValue());
Log.i("---------- Name","::"+obj.getStrValue());
}
}
您需要在Model类中实现Serializable。 然后,要将Model对象从源Activity传递到目标Activity,请在源Activity中使用以下代码:
Model objModel = new Model();
Intent modelActivity = (Model.this, detail.class);
intent.putExtra("ModelObject", objModel);
并且目标Activity中的此代码:
Model modelObject = getIntent().getSerializableExtra("ModelObject");
嗨,感谢您的回复...但是当在目标活动中收到与语句中的错误相同的对象时... model = this.getIntent()。getSerializableExtra("ModelClass");
什么是错误或logcat输出相同?
语法错误发生....
然后问题就在你的代码中。 检查一下。 你应该写:Model modelObject = getIntent()。getSerializableExtra("ModelObject");
是的,我已经编写了与您描述的相同的代码...语法错误是转换错误。 更改模型对象是可序列化的......我还在我的类Model中实现了serial ..
根据你的上述评论,根据这个评论goo.gl/DsUTT,你可以试试这个:Model modelObject =(Model)getIntent()。getSerializableExtra("ModelObject");
您的类应该实现Parcelable接口,然后您可以使用以下命令将其发送到另一个Activity:
MyClass myObject;
//...
intent=new Intent(this, MyAnotherActivity.class);
intent.putExtra("mydata", (Parcelable )myObject);
this.startActivity(intent);
本文详细介绍了如何在Android应用中通过Intent传递自定义对象,包括使用Parcelable和Serializable接口的方法。首先展示了如何实现Parcelable接口的一个示例类,然后在源Activity中创建对象并放入Intent,最后在目标Activity中接收并解析该对象。还提到了使用Serializable接口的替代方案,并提供了处理潜在错误的提示。
2123

被折叠的 条评论
为什么被折叠?



