From: http://www.eoeandroid.com/thread-161416-1-1.html
Simple data transfer between activities may be frequently used. Today, an arraylist <myclass> is required.
It took a little time to get it done and learned something. Record the following again.
1. Use the serializable Method
It is common to serialize an instance of a class and then store or transmit it in Java, which is also available in Android.
The code is simple.
public class MyClass implements Serializable{ private static final long serialVersionUID = 1L; public String userName; public String psw; public int age;}
A custom class that implements the serializable interface.
findViewById(R.id.send_arraylist_button).setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ArrayList<MyClass> arrayList = new ArrayList<MyClass>(); for (int i = 0; i < 10; i++) { MyClass myClass = new MyClass(); myClass.userName = "a" + i; myClass.psw = "b" + i; myClass.age = 10 + i; arrayList.add(myClass); } Intent intent = new Intent(); intent.putExtra("key", arrayList); intent.setClass(MainActivity.this, ResultActivity.class); startActivity(intent); } });
Transmitted in an activity.
ArrayList<MyClass> arrayList = (ArrayList<MyClass>) getIntent().getSerializableExtra("key"); String result = "" ; for (MyClass myClass : arrayList) { result += myClass.userName + "--" + myClass.psw + "--" + myClass.age + "\n"; } ((TextView)findViewById(R.id.show_result_textview)).setText(result);
Receive in another activity
2. Use the parcelable Method
Android memory is limited, forcing it to encapsulate the parcel container to replace the serializable method.
Some comments are made in the code, so I will not explain them here.
/*** Parcel class: http://developer.android.com/reference/android/ OS /Parcel.html <br> * the container that encapsulates data, the encapsulated data can be passed through intent or IPC <br> ** parcelable interface: http://developer.android.com/reference/android/ OS /Parcelable.html <br> * after a custom class inherits this interface, it can be written to or restored from parcel after being instantiated. <Br> ** if a class implements this interface, its object instance can be written to parcel and restored from it, * In addition, this class must have a static field named Creator, which implements parcelable. the object instance of the class of the Creator interface. */Public class myclass2 implements parcelable {Public String username; Public String psw; Public int age; // static parcelable. creator interface public static final parcelable. creator <myclass2> creator = new creator <myclass2> () {// create an instance of the class and obtain data from parcel to instantiate public myclass2 createfromparcel (parcel source) {myclass2 myclass2 = new myclass2 (); myclass2.username = source. readstring (); myclass2.psw = source. readstring (); myclass2.age = source. readint (); Return myclass2;} public myclass2 [] newarray (INT size) {// todo auto-generated method stub return New myclass2 [size] ;}}; // @ override public int describecontents () {// todo auto-generated method stub return 0 ;} // write data to the external provided parcel @ override public void writetoparcel (parcel DEST, int flags) {// todo auto-generated method stub DeST. writestring (username); DeST. writestring (psw); DeST. writeint (AGE );}}
A custom class, which is described in the comments and read the code.
//use Parcelable findViewById(R.id.send_arraylist_button).setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ArrayList<MyClass2> arrayList = new ArrayList<MyClass2>(); for (int i = 0; i < 10; i++) { MyClass2 myClass2 = new MyClass2(); myClass2.userName = "a" + i; myClass2.psw = "b" + i; myClass2.age = 10 + i; arrayList.add(myClass2); } Intent intent = new Intent(); intent.putParcelableArrayListExtra("key", arrayList); intent.setClass(MainActivity.this, ResultActivity.class); startActivity(intent); } });
Send
//use Parcelable ArrayList<MyClass2> arrayList = (ArrayList<MyClass2>) getIntent().getSerializableExtra("key"); String result = "" ; for (MyClass2 myClass2 : arrayList) { result += myClass2.userName + "--" + myClass2.psw + "--" + myClass2.age + "\n"; } ((TextView)findViewById(R.id.show_result_textview)).setText(result);
Receive.