<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Blogger Scott

    Android Intent傳遞對象和ArrayList

    在網上看到一個介紹利用Intent在activity之間傳遞復雜數(shù)據(jù)結構對象(如ArrayList)的例子,經過實測可行,記錄下來,供以后參考。

    感謝wdaming1986整理,本文轉自http://blog.csdn.net/wdaming1986/article/details/6762633

                           程序主界面                                                    點擊list按鈕傳遞數(shù)據(jù)
         


                   點擊parcelable傳遞數(shù)據(jù)                                     點擊serializable傳遞數(shù)據(jù)

           

    MainAcitivty.java的代碼
      1import java.io.Serializable;  
      2import java.util.ArrayList;  
      3import java.util.HashMap;  
      4import java.util.List;  
      5import java.util.Map;  
      6  
      7import android.app.Activity;  
      8import android.app.ListActivity;  
      9import android.content.Intent;  
     10import android.graphics.Color;  
     11import android.graphics.drawable.GradientDrawable;  
     12import android.graphics.drawable.GradientDrawable.Orientation;  
     13import android.os.Bundle;  
     14import android.view.View;  
     15import android.view.View.OnClickListener;  
     16import android.widget.Button;  
     17  
     18public class MainActivity extends Activity implements Serializable{  
     19      
     20    private static final long serialVersionUID = 1L;  
     21      
     22    private String s_name;  
     23    private int s_number;  
     24    private String s_sex;  
     25      
     26    private Button list_Button;  
     27    private Button ser_Button;  
     28    private Button par_Button;  
     29    private ArrayList<String> m_list;  
     30      
     31    public  final static String PAR_KEY = "com.cn.daming.parcelable";  
     32    public  final static String SER_KEY = "com.cn.daming.serializable";  
     33    public  final static String LIST_KEY = "com.cn.daming.ArrayList";  
     34    @Override  
     35    public void onCreate(Bundle savedInstanceState) {  
     36        super.onCreate(savedInstanceState);  
     37        setContentView(R.layout.main);  
     38        initlist();  
     39        drawBackground();  
     40        initList_Button();  
     41        initPar_Button();  
     42        inintSer_Button();  
     43    }
      
     44      
     45    public void initlist()  
     46    {  
     47        m_list = new ArrayList<String>();  
     48        m_list.add("大明ArrayList");  
     49        m_list.add("年齡:25歲");  
     50        m_list.add("性別:男");  
     51    }
      
     52      
     53    public void drawBackground()  
     54    {  
     55        GradientDrawable grad = new GradientDrawable(   
     56                   Orientation.TL_BR,  
     57                   new int[] {Color.rgb(00127),  
     58                              Color.rgb(00255),  
     59                              Color.rgb(1270255),  
     60                              Color.rgb(127127255),  
     61                              Color.rgb(127255255),  
     62                              Color.rgb(255255255)}
       
     63        );   
     64  
     65        this.getWindow().setBackgroundDrawable(grad);  
     66    }
      
     67      
     68    public void initList_Button()  
     69    {  
     70        list_Button = (Button)findViewById(R.id.list_button);  
     71        list_Button.setOnClickListener(new OnClickListener(){  
     72  
     73            @Override  
     74            public void onClick(View arg0) {  
     75                Intent list_intent = new Intent();  
     76                list_intent.putStringArrayListExtra(LIST_KEY, m_list);  
     77                list_intent.setClass(MainActivity.this, ShowListView.class);  
     78                startActivity(list_intent);  
     79            }
      
     80        }
    );  
     81    }
      
     82      
     83    public void initPar_Button()  
     84    {  
     85        par_Button = (Button)findViewById(R.id.par_button);  
     86        par_Button.setOnClickListener(new OnClickListener(){  
     87  
     88            @Override  
     89            public void onClick(View arg0) {  
     90                 Student m_Student = new Student();   
     91                 m_Student.setName("大明例子");  
     92                 m_Student.setAge(25);  
     93                 m_Student.setSex("");  
     94                 Intent p_Intent = new Intent(MainActivity.this,ShowParView.class);    
     95                 Bundle mBundle = new Bundle();    
     96                 mBundle.putParcelable(PAR_KEY, m_Student);  
     97                 p_Intent.putExtras(mBundle);    
     98                 startActivity(p_Intent);    
     99            }
      
    100        }
    );  
    101    }
      
    102      
    103    public void inintSer_Button()  
    104    {  
    105        ser_Button = (Button)findViewById(R.id.ser_button);  
    106        ser_Button.setOnClickListener(new OnClickListener(){  
    107  
    108            @Override  
    109            public void onClick(View arg0) {  
    110                    MainActivity s_activity = new MainActivity();    
    111                    s_activity.setS_name("Daming Serlizable!");    
    112                    s_activity.setS_number(25);   
    113                    s_activity.setS_sex("");    
    114                    Intent mIntent = new Intent(MainActivity.this,ShowSerView.class);    
    115                    Bundle mBundle = new Bundle();    
    116                    mBundle.putInt("state"3);  
    117                    mBundle.putSerializable(SER_KEY, s_activity);  
    118                    mIntent.putExtras(mBundle);    
    119                    startActivity(mIntent);    
    120            }
      
    121        }
    );  
    122    }
      
    123      
    124      
    125    public void setS_name(String s_name) {  
    126        this.s_name = s_name;  
    127    }
      
    128  
    129    public String getS_name() {  
    130        return s_name;  
    131    }
      
    132  
    133    public void setS_number(int s_number) {  
    134        this.s_number = s_number;  
    135    }
      
    136  
    137    public int getS_number() {  
    138        return s_number;  
    139    }
      
    140  
    141    public void setS_sex(String s_sex) {  
    142        this.s_sex = s_sex;  
    143    }
      
    144  
    145    public String getS_sex() {  
    146        return s_sex;  
    147    }
      
    148}

    Student.java代碼
     1import android.os.Parcel;  
     2import android.os.Parcelable;  
     3  
     4public class Student implements Parcelable{  
     5  
     6    private String name;  
     7    private int age;  
     8    private String sex;  
     9      
    10    public String getName() {  
    11        return name;  
    12    }
      
    13    public void setName(String name) {  
    14        this.name = name;  
    15    }
      
    16    public int getAge() {  
    17        return age;  
    18    }
      
    19    public void setAge(int age) {  
    20        this.age = age;  
    21    }
      
    22    public String getSex() {  
    23        return sex;  
    24    }
      
    25    public void setSex(String sex) {  
    26        this.sex = sex;  
    27    }
      
    28      
    29   public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {    
    30        public Student createFromParcel(Parcel source) {    
    31            Student mStudent = new Student();    
    32            mStudent.name = source.readString();    
    33            mStudent.age = source.readInt();    
    34            mStudent.sex = source.readString();    
    35            return mStudent;    
    36        }
        
    37        public Student[] newArray(int size) {    
    38            return new Student[size];    
    39        }
        
    40    }
    ;    
    41  
    42      
    43    @Override  
    44    public int describeContents() {  
    45        // TODO Auto-generated method stub   
    46        return 0;  
    47    }
      
    48    @Override  
    49    public void writeToParcel(Parcel parcel, int arg1) {  
    50        parcel.writeString(name);  
    51        parcel.writeInt(age);  
    52        parcel.writeString(sex);  
    53    }
      
    54}
      
    55

    ShowListView.java代碼 
     1import java.util.ArrayList;  
     2  
     3import android.app.Activity;  
     4import android.content.Intent;  
     5import android.graphics.Color;  
     6import android.graphics.drawable.GradientDrawable;  
     7import android.graphics.drawable.GradientDrawable.Orientation;  
     8import android.os.Bundle;  
     9import android.widget.TextView;  
    10  
    11public class ShowListView extends Activity{  
    12  
    13    private Intent list_intent;  
    14    private ArrayList<String> m_arrayList;  
    15    private TextView list_textview;  
    16      
    17    @Override  
    18    protected void onCreate(Bundle savedInstanceState) {  
    19        super.onCreate(savedInstanceState);  
    20        setContentView(R.layout.show_list_view);  
    21        drawBackground();  
    22        list_textview = (TextView)findViewById(R.id.list_text_view);  
    23        list_intent = getIntent();  
    24        m_arrayList = list_intent.getExtras().getStringArrayList(MainActivity.LIST_KEY);  
    25        m_arrayList.get(0);  
    26        list_textview.setText(m_arrayList.get(0)+" \n"+m_arrayList.get(1)+"\n"+m_arrayList.get(2));  
    27    }
      
    28      
    29    public void drawBackground()  
    30    {  
    31        GradientDrawable grad = new GradientDrawable(   
    32                   Orientation.TL_BR,  
    33                   new int[] {Color.rgb(00127),  
    34                              Color.rgb(00255),  
    35                              Color.r
     1import android.app.Activity;  
     2import android.graphics.Color;  
     3import android.graphics.drawable.GradientDrawable;  
     4import android.graphics.drawable.GradientDrawable.Orientation;  
     5import android.os.Bundle;  
     6import android.widget.TextView;  
     7  
     8public class ShowParView extends Activity{  
     9  
    10    private TextView par_text_view;  
    11      
    12    @Override  
    13    protected void onCreate(Bundle savedInstanceState) {  
    14        super.onCreate(savedInstanceState);  
    15        setContentView(R.layout.show_par_view);  
    16        drawBackground();  
    17          
    18        par_text_view = (TextView)findViewById(R.id.par_text_view);  
    19        Student p_student = (Student)getIntent().getParcelableExtra(MainActivity.PAR_KEY);    
    20        par_text_view.setText("姓名: " + p_student.getName()+"\n"+    
    21                          "年齡: " + p_student.getAge() + "\n" +    
    22                          "性別 : " + p_student.getSex() + "\n" +  
    23                          "類:" + p_student.getClass());    
    24  
    25    }
      
    26      
    27    public void drawBackground()  
    28    {  
    29        GradientDrawable grad = new GradientDrawable(   
    30                   Orientation.TL_BR,  
    31                   new int[] {Color.rgb(00127),  
    32                              Color.rgb(00255),  
    33                              Color.rgb(1270255),  
    34                              Color.rgb(127127255),  
    35                              Color.rgb(127255255),  
    36                              Color.rgb(255255255)}
       
    37        );   
    38  
    39        this.getWindow().setBackgroundDrawable(grad);  
    40    }
      
    41}
      
    42
    gb(
    1270255),  
    36                              Color.rgb(127127255),  
    37                              Color.rgb(127255255),  
    38                              Color.rgb(255255255)}
       
    39        );   
    40  
    41        this.getWindow().setBackgroundDrawable(grad);  
    42    }
      
    43}
      
    44

    ShowParView.java代碼

    posted on 2012-03-02 00:22 江天部落格 閱讀(10385) 評論(0)  編輯  收藏 所屬分類: Android

    主站蜘蛛池模板: 国产一区二区三区在线免费| 久久久久亚洲精品中文字幕| 亚洲国产区男人本色在线观看| 最近2019中文字幕mv免费看| 在线观看亚洲精品专区| 亚洲精品无码久久毛片| 亚洲香蕉免费有线视频| 国产精品亚洲片在线花蝴蝶| 亚洲日韩一区二区三区| 亚洲熟妇av一区二区三区| 成年男女免费视频网站| 特级做A爰片毛片免费69| 成全动漫视频在线观看免费高清版下载 | 亚洲色av性色在线观无码| 免费看小12萝裸体视频国产| 又粗又大又长又爽免费视频| 国产精品视频免费一区二区| 久久久久久久99精品免费| 亚洲成a∧人片在线观看无码| 日本亚洲成高清一区二区三区| 日韩高清免费在线观看| 一级毛片免费毛片一级毛片免费 | 四虎必出精品亚洲高清| 亚洲欧美中文日韩视频| 美女被免费视频网站| 亚洲人成人77777在线播放| 自拍偷自拍亚洲精品第1页| 日韩亚洲精品福利| 麻豆国产VA免费精品高清在线| 无码人妻精品中文字幕免费| 1000部国产成人免费视频| 久久九九全国免费| 免费黄网站在线看| 免费成人激情视频| 日韩视频在线免费| 国产亚洲人成无码网在线观看| 亚洲精品久久久www| 亚洲AV无码码潮喷在线观看| 亚洲老妈激情一区二区三区| 亚洲国产精品SSS在线观看AV| 亚洲性69影院在线观看|