android - using serialization Parcelable

1. The new serialization:

android provides a new type: Parcel. This class is used as the packaged data, the data package may be transmitted through Intent or IPC.

In addition to the basic types, Parcelable only class that implements the interface in order to be placed Parcel.

Parcelable achieve points: the need to achieve three things

1) writeToParcel method. The method Parcel class externally supplied write data is declared as follows:

writeToParcel (Parcel dest, int flags) the specific meaning of the parameters, see javadoc

2) describeContents method. Not get to know what's the use, anyway, can be directly returned 0

3) static Parcelable.Creator interfaces, there are two interfaces of the present method:

createFromParcel (Parcel in) to achieve create an instance of the class in the function from

newArray (int size) to create a type T, the length of the array size, only the word (return new T [size]) to. This estimation method is for external use of the array class deserialize this class.

Example:

Demand: We often need to pass through some of the data between a plurality of members Intent (Activity or-Service), a simple type (e.g., numeric, string) may be placed directly into Intent. Complex types (e.g., in J2ee Bean) Parcelable must implement the interface. Examples are as follows:

package cn.wizhy;

import android.os.Parcel;
import android.os.Parcelable;

public class Phone implements Parcelable{
    String type;
    String company;
    int price;
    public Phone(String t,String c,int p) {
        type=t;
        company=c;
        price=p;
    }
    public Phone() {
        // TODO Auto-generated constructor stub
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public static final Parcelable.Creator<Phone> CREATOR = new Creator<Phone>(){

        @Override
        public Phone createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            Phone cus = new Phone();  
                cus.type = source.readString();  
                cus.company = source.readString();  
                cus.price = source.readInt();  
                return cus;  
        }

        @Override
        public Phone[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Phone[size];
        }  
        
    };
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
        dest.writeString(type);
        dest.writeString(company);
        dest.writeInt(price);
        
    }
}

Activity The first, which is stored in the class configured in Arraylist, and a second pass through the Intent Activity

package cn.wizhy;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Demo extends Activity {
    ArrayList<Phone> info = new ArrayList<Phone>();
    public Phone phone;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        insertPhone();
        Intent intent = new Intent(this,Demo2.class);
//        Bundle bundle = new Bundle();
//        bundle.putSerializable("phone", phone);
       
//        intent.putExtras(bundle);
        phone = new Phone("goole","G1",6000);
        info.add(phone);
        phone = new Phone("apple", "iphone3G", 5000);
        info.add(phone);
       intent.putExtra("phones", info);
        
        startActivity(intent);
    }
    public void insertPhone(){
        phone= new Phone("apple", "iphone3G", 5000);
    }
    
}

The second Activity accept data:

package cn.wizhy;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Demo2 extends Activity {
    ArrayList<Phone> info = new ArrayList<Phone>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        info =intent.getParcelableArrayListExtra("phones");
        for(int i=0;i<info.size();i++){
            System.out.println("type="+info.get(i).type+"   company="+info.get(i).company+" price"+info.get(i).price);
        }
    }
}

from:http://aijiawang-126-com.iteye.com/blog/643762

Reproduced in: https: //www.cnblogs.com/bill-joy/archive/2012/04/25/2470248.html

Guess you like

Origin blog.csdn.net/weixin_33915554/article/details/94048911