【Intent传递】对象、数组

package com.snail.intentobject;



import java.io.Serializable;



public class PersonSer implements Serializable {



    // serialVersionUID的作用是在修改实体类后,可以正常的序列化和反序列化,在此不多说,大家可以谷歌百度下。

    private static final long serialVersionUID = -7620435178023928252L;

    private String name;

    private int age;



    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

}
View Code

 

package com.snail.intentobject;



import android.os.Parcel;

import android.os.Parcelable;

 

public class PersonPar implements Parcelable {



    private String name;

    private int age;



    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }



    // 序列化实体类

    public static final Parcelable.Creator<PersonPar> CREATOR = new Creator<PersonPar>() {

        public PersonPar createFromParcel(Parcel source) {

            PersonPar personPar = new PersonPar();

            personPar.name = source.readString();

            personPar.age = source.readInt();

            return personPar;

        }

        public PersonPar[] newArray(int size) {

            return new PersonPar[size];

        }

    };

    @Override

    public int describeContents() {

        return 0;

    }

    @Override  // 将实体类数据写入Parcel

    public void writeToParcel(Parcel parcel, int flags) {

        parcel.writeString(name);

        parcel.writeInt(age);

    }

}
View Code

    以上是两个实例化的实体类。

 

public class MainActivity extends Activity {



    public static final String SER_KEY = "ser_key";

    public static final String PAR_KEY = "par_key";

    public static final String SER_TYPE = "ser_type";

    public static final String PAR_TYPE = "par_type";

    public static final String LIST_KEY = "list_key";

    public static final String LIST_TYPE = "list_type";

    private ArrayList<String> m_list;



    private Button mSerBtn;

    private Button mParBtn;

    private Button list_Button;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initViews();

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main, menu);

        return true;

    }



    private void initViews() {

        mSerBtn = (Button) findViewById(R.id.serializable_btn);

        mSerBtn.setOnClickListener(onClickListener);

        mParBtn = (Button) findViewById(R.id.parcelable_btn);

        mParBtn.setOnClickListener(onClickListener);

        list_Button = (Button) findViewById(R.id.list_button);

        list_Button.setOnClickListener(onClickListener);



        m_list = new ArrayList<String>();

        m_list.add("大明ArrayList");

        m_list.add("年龄:25岁");

        m_list.add("性别:男");

    }



    OnClickListener onClickListener = new OnClickListener() {



        @Override

        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, DestActivity.class);

            Bundle bundle = new Bundle();

            switch (v.getId()) {

            // Serializable Object

            case R.id.serializable_btn:

                // Serializable传递对象

                PersonSer personSer = new PersonSer();

                personSer.setName("zuolong");

                personSer.setAge(26);

                bundle.putSerializable(SER_KEY, personSer);

                intent.putExtra("type", SER_TYPE);

                intent.putExtras(bundle);

                startActivity(intent);

                break;

            // Parcelable Object

            case R.id.parcelable_btn:

                // Parcelable传递对象

                PersonPar personPar = new PersonPar();

                personPar.setName("snail");

                personPar.setAge(27);

                // Bundle有putParcelableArray和putParcelableArrayList方法,也就可以传递数组和列表

                bundle.putParcelable(PAR_KEY, personPar);

                intent.putExtra("type", PAR_TYPE);

                // Intent也有putParcelableArrayListExtra方法,可以传递实现Parcelable接口的对象列表

                intent.putExtras(bundle);

                startActivity(intent);

                break;

            case R.id.list_button: // Intent传递list

                Intent list_intent = new Intent();

                list_intent.putStringArrayListExtra(LIST_KEY, m_list);

                // list_intent.putExtra("type", LIST_TYPE);

                // list_intent.putExtras(bundle);

                list_intent.setClass(MainActivity.this,

                        Dest_ArrayList_Activity.class);

                startActivity(list_intent);

                break;

            }

        }

    };



    @Override

    public boolean dispatchKeyEvent(android.view.KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {

            return true;

        }

        return super.dispatchKeyEvent(event);

    };



}
View Code

 

 

/** Intent传递对象过来的详细信息 */

public class DestActivity extends Activity {



    private TextView mDestTestView;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dest);



        initViews();

        bindData();

    }

    



    private void initViews() {

        mDestTestView = (TextView) findViewById(R.id.dest_text_view);

    }



    private void bindData() {

        StringBuffer sb = new StringBuffer();

        String type = getIntent().getStringExtra("type");

        // 根据type判断是哪种类型

        if (type.equals(MainActivity.SER_TYPE)) {

            // 获取Serializable对象

            PersonSer personSer = (PersonSer) getIntent().getSerializableExtra(

                    MainActivity.SER_KEY);

            sb.append("----From Serializable----").append("\n");

            sb.append("Name:").append(personSer.getName()).append("\n");

            sb.append("Age:").append(personSer.getAge()).append("\n");

        } else if (type.equals(MainActivity.PAR_TYPE)) {

            // 获取Parcelable对象

            PersonPar personPar = (PersonPar) getIntent().getParcelableExtra(

                    MainActivity.PAR_KEY);

            sb.append("----From Parcelable----").append("\n");

            sb.append("Name:").append(personPar.getName()).append("\n");

            sb.append("Age:").append(personPar.getAge()).append("\n");

        }  



        mDestTestView.setText(sb.toString());

    }

}
View Code

 

/** Intent传递list过来的详细信息 */

public class Dest_ArrayList_Activity extends Activity {



    private TextView mDestTestView;

    private Intent list_intent; 

    private ArrayList<String> m_arrayList; 



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dest);



        initViews();

    //    bindData();

        

        list_intent = getIntent(); 

        m_arrayList = list_intent.getExtras().getStringArrayList( 

                MainActivity.LIST_KEY); 

        m_arrayList.get(0); 

        mDestTestView.setText(m_arrayList.get(0) + " \n" + m_arrayList.get(1) 

                + "\n" + m_arrayList.get(2)); 

    }

    



    private void initViews() {

        mDestTestView = (TextView) findViewById(R.id.dest_text_view);

    }



/*    private void bindData() {

        StringBuffer sb = new StringBuffer();

        String type = getIntent().getStringExtra("type");

        // 根据type判断是哪种类型

        if (type.equals(MainActivity.SER_TYPE)) {

            // 获取Serializable对象

            PersonSer personSer = (PersonSer) getIntent().getSerializableExtra(

                    MainActivity.SER_KEY);

            sb.append("----From Serializable----").append("\n");

            sb.append("Name:").append(personSer.getName()).append("\n");

            sb.append("Age:").append(personSer.getAge()).append("\n");

        } else if (type.equals(MainActivity.PAR_TYPE)) {

            // 获取Parcelable对象

            PersonPar personPar = (PersonPar) getIntent().getParcelableExtra(

                    MainActivity.PAR_KEY);

            sb.append("----From Parcelable----").append("\n");

            sb.append("Name:").append(personPar.getName()).append("\n");

            sb.append("Age:").append(personPar.getAge()).append("\n");

        }  



        mDestTestView.setText(sb.toString());

    }*/

}
View Code

 

完整DEMO下载地址:http://download.csdn.net/detail/androidsj/5500621

你可能感兴趣的:(intent)