0

我有这个ArrayList<ToLet> toLet;

ToLet 类是 POJO 类。

现在我怎样才能将它从一项活动传递到另一项活动?最好的方法是什么?

我已经浏览了以下链接..

如何在Android上将对象从一个活动传递到另一个活动

如何在 Android 中将 ArrayList<Custom_Object> 从一个活动传递到另一个活动?

但没有帮助我。所以如果有人知道答案,请告诉我

4

2 回答 2

1

您也可以使用它来传递 ArrayList tolet;一项活动到另一项活动。

创建类的对象:-

ToLet obj = new ToLet();

ArrayList<ToLet> tolet;
int size = tolet.getSize();    
Intent ii = new Intent(your_current_class.this, next_class_where_you_want_to_use);
ii.putExtra("listsize",size);

 startActivity(ii);

现在去你的下一堂课并使用:-

Intent intent = getIntent();
String mylistsize = intent.getIntExtra("listsize",default value);

和 AndroidMainest.xml 文件更新此活动。

<activity android:name="yourcurrentclass" />
<activity android:name="yournextclass" />

它应该可以解决您的查询。

于 2013-02-15T06:45:04.293 回答
0

当您创建意图对象时,您可以利用以下两种方法在两个活动之间传递对象。

把Parceble

putSerializable

下面告诉你关于putParceble

仔细查看为 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];
        }
    };
}

然后制作一个用于将对象发送到另一个活动的活动。

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());
    }
}
于 2013-02-15T06:39:18.907 回答