Android Intent傳遞對象和ArrayList【大明進化十八】 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

以前做項目的時候,需要Intent 傳遞復雜類型,例如:數組,ArrayList類型,傳遞類對象,今天抽空寫瞭個例子,方便以後用的時候查詢!有問題可以留言;

轉載請標明出處:

http://blog.csdn.net/wdaming1986/article/details/6762633

 

                     程序主界面:                                       點擊list按鈕傳遞數據:    

               

 

                  點擊parcelable傳遞數據:    點擊serializable傳遞數據:

                                                             

 

 

下面看代碼:

一、MainAcitivty。java類的代碼:

 

<span style="font-size:16px;color:#000000;">package com.cn.daming; 

 

import java.io.Serializable; 

import java.util.ArrayList; 

import java.util.HashMap; 

import java.util.List; 

import java.util.Map; 

 

import android.app.Activity; 

import android.app.ListActivity; 

import android.content.Intent; 

import android.graphics.Color; 

import android.graphics.drawable.GradientDrawable; 

import android.graphics.drawable.GradientDrawable.Orientation; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

 

public class MainActivity extends Activity implements Serializable{ 

     

    private static final long serialVersionUID = 1L; 

     

    private String s_name; 

    private int s_number; 

    private String s_sex; 

      

    private Button list_Button; 

    private Button ser_Button; 

    private Button par_Button; 

    private ArrayList<String> m_list; 

     

    public  final static String PAR_KEY = "com.cn.daming.parcelable"; 

    public  final static String SER_KEY = "com.cn.daming.serializable"; 

    public  final static String LIST_KEY = "com.cn.daming.ArrayList"; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        initlist(); 

        drawBackground(); 

        initList_Button(); 

        initPar_Button(); 

        inintSer_Button(); 

    } 

     

    public void initlist() 

    { 

        m_list = new ArrayList<String>(); 

        m_list.add("大明ArrayList"); 

        m_list.add("年齡:25歲"); 

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

    } 

     

    public void drawBackground() 

    { 

        GradientDrawable grad = new GradientDrawable(  

                   Orientation.TL_BR, 

                   new int[] {Color.rgb(0, 0, 127), 

                              Color.rgb(0, 0, 255), 

                              Color.rgb(127, 0, 255), 

                              Color.rgb(127, 127, 255), 

                              Color.rgb(127, 255, 255), 

                              Color.rgb(255, 255, 255)}  

        );  

 

        this.getWindow().setBackgroundDrawable(grad); 

    } 

     

    public void initList_Button() 

    { 

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

        list_Button.setOnClickListener(new OnClickListener(){ 

 

            @Override 

            public void onClick(View arg0) { 

                Intent list_intent = new Intent(); 

                list_intent.putStringArrayListExtra(LIST_KEY, m_list); 

                list_intent.setClass(MainActivity.this, ShowListView.class); 

                startActivity(list_intent); 

            } 

        }); 

    } 

     

    public void initPar_Button() 

    { 

        par_Button = (Button)findViewById(R.id.par_button); 

        par_Button.setOnClickListener(new OnClickListener(){ 

 

            @Override 

            public void onClick(View arg0) { 

                 Student m_Student = new Student();  

                 m_Student.setName("大明例子"); 

                 m_Student.setAge(25); 

                 m_Student.setSex("男"); 

                 Intent p_Intent = new Intent(MainActivity.this,ShowParView.class);   

                 Bundle mBundle = new Bundle();   

                 mBundle.putParcelable(PAR_KEY, m_Student); 

                 p_Intent.putExtras(mBundle);   

                 startActivity(p_Intent);   

            } 

        }); 

    } 

     

    public void inintSer_Button() 

    { 

        ser_Button = (Button)findViewById(R.id.ser_button); 

        ser_Button.setOnClickListener(new OnClickListener(){ 

 

            @Override 

            public void onClick(View arg0) { 

                    MainActivity s_activity = new MainActivity();   

                    s_activity.setS_name("Daming Serlizable!");   

                    s_activity.setS_number(25);  

                    s_activity.setS_sex("男");   

                    Intent mIntent = new Intent(MainActivity.this,ShowSerView.class);   

                    Bundle mBundle = new Bundle();   

                    mBundle.putInt("state", 3); 

                    mBundle.putSerializable(SER_KEY, s_activity); 

                    mIntent.putExtras(mBundle);   

                    startActivity(mIntent);   

            } 

        }); 

    } 

     

     

    public void setS_name(String s_name) { 

        this.s_name = s_name; 

    } 

 

    public String getS_name() { 

        return s_name; 

    } 

 

    public void setS_number(int s_number) { 

        this.s_number = s_number; 

    } 

 

    public int getS_number() { 

        return s_number; 

    } 

 

    public void setS_sex(String s_sex) { 

        this.s_sex = s_sex; 

    } 

 

    public String getS_sex() { 

        return s_sex; 

    } 

}</span> 

 

二、Student。java類的代碼:

 

package com.cn.daming; 

 

import android.os.Parcel; 

import android.os.Parcelable; 

 

public class Student implements Parcelable{ 

 

    private String name; 

    private int age; 

    private String sex; 

     

    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 String getSex() { 

        return sex; 

    } 

    public void setSex(String sex) { 

        this.sex = sex; 

    } 

     

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

        public Student createFromParcel(Parcel source) {   

            Student mStudent = new Student();   

            mStudent.name = source.readString();   

            mStudent.age = source.readInt();   

            mStudent.sex = source.readString();   

            return mStudent;   

        }   

        public Student[] newArray(int size) {   

            return new Student[size];   

        }   

    };   

 

     

    @Override 

    public int describeContents() { 

        // TODO Auto-generated method stub 

        return 0; 

    } 

    @Override 

    public void writeToParcel(Parcel parcel, int arg1) { 

        parcel.writeString(name); 

        parcel.writeInt(age); 

        parcel.writeString(sex); 

    } 

 

三、ShowListView。java類的代碼:

 

package com.cn.daming; 

 

import java.util.ArrayList; 

 

import android.app.Activity; 

import android.content.Intent; 

import android.graphics.Color; 

import android.graphics.drawable.GradientDrawable; 

import android.graphics.drawable.GradientDrawable.Orientation; 

import android.os.Bundle; 

import android.widget.TextView; 

 

public class ShowListView extends Activity{ 

 

    private Intent list_intent; 

    private ArrayList<String> m_arrayList; 

    private TextView list_textview; 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.show_list_view); 

        drawBackground(); 

        list_textview = (TextView)findViewById(R.id.list_text_view); 

        list_intent = getIntent(); 

        m_arrayList = list_intent.getExtras().getStringArrayList(MainActivity.LIST_KEY); 

        m_arrayList.get(0); 

        list_textview.setText(m_arrayList.get(0)+" \n"+m_arrayList.get(1)+"\n"+m_arrayList.get(2)); 

    } 

     

    public void drawBackground() 

    { 

        GradientDrawable grad = new GradientDrawable(  

                   Orientation.TL_BR, 

                   new int[] {Color.rgb(0, 0, 127), 

                              Color.rgb(0, 0, 255), 

                              Color.rgb(127, 0, 255), 

                              Color.rgb(127, 127, 255), 

                              Color.rgb(127, 255, 255), 

                              Color.rgb(255, 255, 255)}  

        );  

 

        this.getWindow().setBackgroundDrawable(grad); 

    } 

 

四、ShowParView。java類的代碼:

 

package com.cn.daming; 

 

import android.app.Activity; 

import android.graphics.Color; 

import android.graphics.drawable.GradientDrawable; 

import android.graphics.drawable.GradientDrawable.Orientation; 

import android.os.Bundle; 

import android.widget.TextView; 

 

public class ShowParView extends Activity{ 

 

    private TextView par_text_view; 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.show_par_view); 

        drawBackground(); 

         

        par_text_view = (TextView)findViewById(R.id.par_text_view); 

        Student p_student = (Student)getIntent().getParcelableExtra(MainActivity.PAR_KEY);   

        par_text_view.setText("姓名: " + p_student.getName()+"\n"+   

                          "年齡: " + p_student.getAge() + "\n" +   

                          "性別: " + p_student.getSex() + "\n" + 

                          "類:" + p_student.getClass());   

 

    } 

     

    public void drawBackground() 

    { 

        GradientDrawable grad = new GradientDrawable(  

                   Orientation.TL_BR, 

                   new int[] {Color.rgb(0, 0, 127), 

                              Color.rgb(0, 0, 255), 

                              Color.rgb(127, 0, 255), 

                              Color.rgb(127, 127, 255), 

                              Color.rgb(127, 255, 255), 

                              Color.rgb(255, 255, 255)}  

        );  

 

        this.getWindow().setBackgroundDrawable(grad); 

    } 

 

五、ShowSerView。java類的代碼:

 

package com.cn.daming; 

 

import android.app.Activity; 

import android.graphics.Color; 

import android.graphics.drawable.GradientDrawable; 

import android.graphics.drawable.GradientDrawable.Orientation; 

import android.os.Bundle; 

import android.widget.TextView; 

 

public class ShowSerView extends Activity{ 

 

    private TextView ser_text_view; 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.show_ser_view); 

        drawBackground(); 

         

        ser_text_view = (TextView)findViewById(R.id.ser_text_view); 

        MainActivity s_activity = (MainActivity)getIntent().getSerializableExtra(MainActivity.SER_KEY);    

        ser_text_view.setText("You name is: " + s_activity.getS_name() + "\n"+   

                "You age is: " + s_activity.getS_number() +"\n"+ 

                "You sex is: " + s_activity.getS_sex());   

    } 

     

    public void drawBackground() 

    { 

        GradientDrawable grad = new GradientDrawable(  

                   Orientation.TL_BR, 

                   new int[] {Color.rgb(0, 0, 127), 

                              Color.rgb(0, 0, 255), 

                              Color.rgb(127, 0, 255), 

                              Color.rgb(127, 127, 255), 

                              Color.rgb(127, 255, 255), 

                              Color.rgb(255, 255, 255)}  

        );  

 

        this.getWindow().setBackgroundDrawable(grad); 

    } 

 

xml佈局文件

一、main。xml佈局文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

    <TextView   

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:text="@string/hello" 

        android:textSize="12pt" 

     /> 

     <Button 

        android:id="@+id/list_button" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:layout_marginTop="10dip" 

        android:text="Intent傳遞list" 

        android:textSize="12pt" 

     /> 

     <Button 

        android:id="@+id/par_button" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:layout_marginTop="10dip" 

        android:layout_gravity="center" 

        android:text="parcelable傳遞對象" 

        android:textSize="12pt" 

     /> 

     <Button 

        android:id="@+id/ser_button" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:layout_marginTop="10dip" 

        android:text="serializable傳遞對象" 

        android:textSize="12pt" 

     /> 

</LinearLayout> 

 

二、show_list_view.xml佈局文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

     <TextView   

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_marginBottom="5dip" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:text="傳遞過來的ArrayList的值" 

        android:textSize="12pt" 

     /> 

     <TextView   

        android:id="@+id/list_text_view" 

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_marginBottom="10dip" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:textSize="12pt" 

     /> 

</LinearLayout> 

 

三、show_par_view.xml佈局文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

    <TextView   

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_gravity="center_horizontal" 

        android:layout_marginBottom="10dip" 

        android:gravity="center_horizontal"  

        android:text="接受從MainActivity中傳遞過來的對象" 

        android:textSize="12pt" 

     /> 

     <TextView   

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_marginBottom="5dip" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:text="傳遞過來的Parcelable值" 

        android:textSize="12pt" 

     /> 

     <TextView   

        android:id="@+id/par_text_view" 

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:textSize="12pt" 

     /> 

</LinearLayout> 

 

四、show_ser_view.xml佈局文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    > 

     <TextView   

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_marginBottom="5dip" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:text="傳遞過來的Serializable值" 

        android:textSize="12pt" 

     /> 

     <TextView   

        android:id="@+id/ser_text_view" 

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content" 

        android:layout_marginBottom="10dip" 

        android:layout_gravity="center_horizontal" 

        android:gravity="center_horizontal"  

        android:textSize="12pt" 

     /> 

</LinearLayout> 

Manifest.xml佈局文件:

 

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

      package="com.cn.daming" 

      android:versionCode="1" 

      android:versionName="1.0"> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

        <activity android:name=".MainActivity" 

                  android:label="@string/app_name"> 

            <intent-filter> 

                <action android:name="android.intent.action.MAIN" /> 

                <category android:name="android.intent.category.LAUNCHER" /> 

            </intent-filter> 

        </activity> 

        <activity android:name=".ShowListView"/> 

        <activity android:name=".ShowParView"/> 

        <activity android:name=".ShowSerView"/> 

    </application> 

    <uses-sdk android:minSdkVersion="8" /> 

 

</manifest>    

發佈留言