Android 如何将一个对象传递给另一个活动?

Android 如何将一个对象传递给另一个活动?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我需要通过intent将一个类对象传递给另一个活动。这是我的班级代码: public class Model { private String Name; private ArrayList<Trim> trim; public String getName() { return Name; } public void setName(String Name) { this.Name = Na

我需要通过intent将一个类对象传递给另一个活动。这是我的班级代码:

public class Model
{
    private String Name;
    private ArrayList<Trim> trim;

    public String getName()
    {
        return Name;
    }

    public void setName(String Name)
    {
        this.Name = Name;
    }

    public ArrayList<Trim> getTrim()
    {
        return trim;
    }

    public void setTrim(ArrayList<Trim> trim)
    {
        this.trim = trim;
    }
}
公共类模型
{
私有字符串名称;
私人ArrayList trim;
公共字符串getName()
{
返回名称;
}
公共void集合名(字符串名)
{
this.Name=Name;
}
公共数组列表getTrim()
{
回位微调;
}
公共空间设置修剪(ArrayList修剪)
{
this.trim=修剪;
}
}

要将对象传递给另一个活动,您需要实现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];
        }
    };
}
然后创建一个用于将对象发送到另一个活动的活动

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);
            }
        });
    }
}
现在,最后让另一个活动读取对象并从中获取值

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());
    }
}

您的类应该实现
Parcelable
接口,然后您可以使用以下方法将其发送到另一个
活动

MyClass myObject;
//...
intent=new Intent(this, MyAnotherActivity.class);
intent.putExtra("mydata", (Parcelable )myObject);
this.startActivity(intent);

您需要在
模型
类中实现
可序列化
。然后,要将
模型
对象从源
活动
传递到目标
活动
,请在源
活动
中使用以下代码:

Model objModel = new Model();
Intent modelActivity = (Model.this, detail.class);
intent.putExtra("ModelObject", objModel);
Model modelObject = getIntent().getSerializableExtra("ModelObject");
而目标
活动中的此代码

Model objModel = new Model();
Intent modelActivity = (Model.this, detail.class);
intent.putExtra("ModelObject", objModel);
Model modelObject = getIntent().getSerializableExtra("ModelObject");

这个密码里有什么?发布活动代码可能重复:使用Parcelable或serializable实现。嗨,谢谢你的回复。。但当在目标活动中接收到与语句中的错误相同的对象时。。。model=this.getIntent().getSerializableExtra(“ModelClass”);相同的错误或logcat输出是什么?那么问题出在代码中。好好检查一下。您应该编写:modelmodelobject=getIntent().getSerializableExtra(“modelObject”);是的,我写了和你描述的相同的代码。。。语法错误是强制转换错误。更改模型对象是可序列化的…我还实现了串行。。在我的类模型中。根据您的上述注释和此注释,您可以尝试以下操作:ModelModelObject=(Model)getIntent().getSerializableExtra(“modelObject”);