如何传递一个对象到另一个活动? [英] How to pass an object to another activity?

查看:118
本文介绍了如何传递一个对象到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个类的对象,通过意图传递给另一个活动。这里是我的课code:

 公共类模型
{
    私人字符串名称;
    私人的ArrayList<修剪>修剪;

    公共字符串的getName()
    {
        返回名称;
    }

    公共无效setname可以(字符串名称)
    {
        this.Name =名称;
    }

    公众的ArrayList<修剪> getTrim()
    {
        返回修剪;
    }

    公共无效setTrim(ArrayList中<修剪>修剪)
    {
        this.trim =修剪;
    }
}
 

解决方案

要传递一个对象,你需要实现Parcelable另一个活动。

审议的 写作Parcelable类为Android 的谨慎。在这里,他们正在使用HashMap来存储值和对象传递给另一个类。


请一个类,对象A 。在此,我使用所有的setter和getter方法​​。

 包com.ParcableExample.org;

进口android.os.Parcel;
进口android.os.Parcelable;

/ **
 *该任务可以被分配到一个基本目标
 *对象之间传递。
 * /

公共类对象A实现Parcelable
{
    私人字符串将strValue = NULL;
    私人诠释的intValue = 0;

    / **
     *标准的基本构造非包裹
     *对象的创建。
     * /

    公共对象A()
    {
    }

    / **
     *
     *构造函数的时候重新构造对象使用
     *从一个包裹。
     *
     *参数在从中读取此对象的包裹。
     * /

    公共对象A(包裹中)
    {
        readFromParcel(在);
    }

    / **
     *标准的getter
     *
     * @返回将strValue
     * /
    公共字符串getStrValue()
    {
        返回this.strValue;
    }

    / **
     *标准的制定者
     *
     * @参数strValue中
     * /

    公共无效setStrValue(字符串将strValue)
    {
        this.strValue = strValue的;
    }


    / **
     *标准的getter
     *
     * @返回的intValue
     * /
    公共整数getIntValue()
    {
        返回this.intValue;
    }

    / **
     *标准的制定者
     *
     * @参数strValue中
     * /
    公共无效setIntValue(整型的intValue)
    {
        this.intValue =的intValue;
    }

    @覆盖
    公众诠释describeContents()
    {
        返回0;
    }

    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志)
    {
        //我们只需要编写每个字段进入
        // 包。当我们从包裹看,他们
        //会回来的相同顺序

        dest.writeString(this.strValue);
        dest.writeInt(this.intValue);
    }

    / **
     *
     *从构造函数调用来创建此
     *从一个包裹对象。
     *
     * @参数在包裹从中重新创建对象。
     * /
    公共无效readFromParcel(包裹中)
    {
        //我们只需要读回每个
        //顺序领域,这是
        //写入包裹

        this.strValue = in.readString();
        this.intValue = in.readInt();
    }

    / **
    *
    ※这是所需要的机器人能够
    *创建新的对象,单独或数组。
    *
    *这也意味着您可以使用使用默认
    *构造函数来创建对象,并使用另一个
    *方法,它需要hyrdate。
    * /
    @燮pressWarnings(未登记)
    公共静态最终Parcelable.Creator CREATOR =新Parcelable.Creator()
    {
        @覆盖
        公共对象A createFromParcel(包裹中)
        {
            返回新对象A(中);
        }

        @覆盖
        公共对象[] newArray(INT尺寸)
        {
            返回新对象A【尺寸】;
        }
    };
}
 

然后,让用于对象发送到另一个活动的一项活动。

 包com.ParcableExample.org;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类ParcableExample扩展活动
{
    私人按钮btnClick;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        initControls();
    }

    私人无效initControls()
    {
        btnClick =(按钮)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(新OnClickListener()
        {
            @覆盖
            公共无效的onClick(查看arg0中)
            {
                对象A的obj =新对象A();
                obj.setIntValue(1);
                obj.setStrValue(奇拉格);

                意图I =新的意图(ParcableExample.this,MyActivity.class);
                i.putExtra(com.package.ObjectA,OBJ);
                startActivity(ⅰ);
            }
        });
    }
}
 

现在终于做出一个阅读对象另一个活动,并从获得的价值。

 包com.ParcableExample.org;

进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;

公共类MyActivity扩展活动
{
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        束束= getIntent()getExtras()。
        对象A OBJ = bundle.getParcelable(com.package.ObjectA);

        Log.i(----------标识,::+ obj.getIntValue());
        Log.i(----------名称,::+ obj.getStrValue());
    }
}
 

I need to pass a class object to another activity via intent. Here is my class code:

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

解决方案

To pass an object to another activity you need to implement Parcelable.

Review Writing Parcelable classes for Android carefully. Here they are using Hashmap to store the values and pass the object to another class.

OR


Make one class, ObjectA. In that, I used all the setter and getter methods.

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];
        }
    };
}

Then make one Activity that is used to send the Object to another 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);
            }
        });
    }
}

Now finally make one another activity that read the Object and get the value from that.

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

这篇关于如何传递一个对象到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆