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

android– 如何将对象传递给另一个活动?,第1张

概述参见英文答案>HowtosendanobjectfromoneAndroidActivitytoanotherusingIntents?                                    35个我需要通过intent将类对象传递给另一个活动.这是我的班级代码:publicclassModel{privateStr

参见英文答案 > How to send an object from one Android Activity to another using Intents?                                    35个
我需要通过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;    }}

解决方法:

要将对象传递给另一个活动,您需要实现Parcelable.

仔细阅读Writing Parcelable classes for Android.在这里,他们使用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());    }}
总结

以上是内存溢出为你收集整理的android – 如何将对象传递给另一个活动?全部内容,希望文章能够帮你解决android – 如何将对象传递给另一个活动?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096742.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存