Feb 20, 2014

Android activity pass object array

 Your object must implements Parcelable

package model;

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

public class OrderHead implements Parcelable  {

    int idx;
    String OrderNum;  
    String CustID;
    long OrderDate;
    boolean Uploaded;
    // constructors
    public OrderHead() {

    }   
    public OrderHead(Parcel in){
        readFromParcel(in);
   
    }
    public OrderHead(int idx,String OrderNum, String CustID) {
        this.idx = idx;
        this.OrderNum = OrderNum;
        this.CustID = CustID;
        this.Uploaded = false;
    }
    public OrderHead(String OrderNum, String CustID,long OrderDate) {       
        this.OrderNum = OrderNum;
        this.CustID = CustID;
        this.OrderDate = OrderDate;
        this.Uploaded = false;
    }
    // setter
    public void setIdx(int id) {
        this.idx = id;
    }
    public void setOrderNum(String OrderNum) {
        this.OrderNum = OrderNum;
    }
    public void setCustID(String CustID) {
        this.CustID = CustID;
    }
    public void setOrderDate(long OrderDate) {
        this.OrderDate = OrderDate;
    }
    public void setUploaded(boolean Uploaded) {
        this.Uploaded = Uploaded;
    }
    // getter
    public String getOrderNum() {
        return this.OrderNum;
    }
    public String getCustID() {
        return this.CustID;
    }
    public long getOrderDate() {
        return this.OrderDate;
    }
    public boolean getUploaded() {
        return this.Uploaded;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        // TODO Auto-generated method stub
       
        arg0.writeInt(idx);
        arg0.writeString(OrderNum);
        arg0.writeString(CustID);
        arg0.writeLong(OrderDate);
        arg0.writeInt(Uploaded==true ? 1:0);        
    }
   
    private void readFromParcel(Parcel in) {  
        idx = in.readInt();
        OrderNum = in.readString();
        CustID = in.readString();
        OrderDate = in.readLong();
        Uploaded = in.readInt()==1 ? true:false;
       
    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public OrderHead createFromParcel(Parcel in) {
            return new OrderHead(in);
            }  
        public OrderHead[] newArray(int size) {
            return new OrderHead[size];
            }
        };
}

send the data object
Intent intent = new Intent(AddNewOrder.this, OrderDetails.class);
                            OrderHead obj;
obj.setUploaded(false);
                            Bundle bundle = new Bundle();
                            bundle.putParcelable("OrderHead", obj);                                  
                            intent.putExtras(bundle);
                            startActivity(intent);      
Send list object

  Intent intent = new Intent(AddNewOrder.this, OrderDetails.class);
                            ArrayList<OrderHead> OrderArraylist = new ArrayList<OrderHead>();
                            OrderArraylist.add(obj);
                            Bundle bundle = new Bundle();
                            bundle.putParcelableArray("OrderHead", obj);                                  
                            intent.putExtras(bundle);
                            startActivity(intent);   


get object
OrderHead objOrderHead = new OrderHead();     
Bundle extras = getIntent().getExtras();        
        objOrderHead  = extras.getParcelable("OrderHead"); 

get list object
ArrayList<OrderHead> lstObj = new ArrayList<OrderHead>();    
Bundle extras = getIntent().getExtras(); 
lstObj = extras.getParcelableArrayList("OrderHead"); 
       

0 comments:

Post a Comment

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib