• Activity使用Serializable传递对象实例


    1. public class SerializableBook implements Serializable {  
    2.     private static final long serialVersionUID = 4226755799531293257L;  
    3.       
    4.     private String Name;  
    5.     private String Author;  
    6.     private String Pubdate;  
    7.     private float Price;  
    8.       
    9.       
    10.     public void setName(String name) {  
    11.         Name = name;  
    12.     }  
    13.       
    14.     public String getName() {  
    15.         return Name;  
    16.     }  
    17.       
    18.     public void setAuthor(String author) {  
    19.         Author = author;  
    20.     }  
    21.       
    22.     public String getAuthor() {  
    23.         return Author;  
    24.     }  
    25.       
    26.     public void setPubdate(String pubdate) {  
    27.         Pubdate = pubdate;  
    28.     }  
    29.       
    30.     public String getPubdate() {  
    31.         return Pubdate;  
    32.     }  
    33.       
    34.     public void setPrice(float price) {  
    35.         Price = price;  
    36.     }  
    37.       
    38.     public float getPrice() {  
    39.         return Price;  
    40.     }  
    41.   
    42. }  

            然后起一个Activity A,这都是和之前的Activity介绍的例子一样,代码如下:

    Java代码
    1. public class ActivityA extends Activity {  
    2.     private String SerializableKey = "ourunix_serialzable";  
    3.     private Button mButton;  
    4.       
    5.       
    6.     @Override  
    7.     public void onCreate(Bundle savedInstanceState) {  
    8.         super.onCreate(savedInstanceState);  
    9.         setContentView(R.layout.layout_for_a);  
    10.           
    11.         initView();  
    12.   
    13.         mButton.setOnClickListener(new OnClickListener() {  
    14.   
    15.             @Override  
    16.             public void onClick(View v) {  
    17.                 // TODO Auto-generated method stub  
    18.                 tranSerializableObject();  
    19.             }  
    20.         });  
    21.     }  
    22.       
    23.     public void initView(){  
    24.         mButton = (Button) findViewById(R.id.a_button);  
    25.         mButton.setText("A跳B");  
    26.     }  
    27.       
    28.     public void tranSerializableObject(){  
    29.         Intent in = new Intent();  
    30.         in.setClass(ActivityA.this, ActivityB.class);  
    31.           
    32.         //实例化一个SerializableBook对象  
    33.         SerializableBook book = new SerializableBook();  
    34.         book.setAuthor("walfred");  
    35.         book.setName("How to learn Android");  
    36.         book.setPrice(10.00f);  
    37.         book.setPubdate("2014-01-01");  
    38.           
    39.         Bundle extras = new Bundle();  
    40.         extras.putSerializable(SerializableKey, book);  
    41.           
    42.         in.putExtras(extras);  
    43.         startActivity(in);  
    44.     }  
    45. }  

            最后在Activity B中接受这个对象,并展示出来,代码如下:

    Java代码
    1. public class ActivityB extends Activity {  
    2.     private String SerializableKey = "ourunix_serialzable";  
    3.     private TextView mTextView;  
    4.     @Override  
    5.     protected void onCreate(Bundle savedInstanceState) {  
    6.         // TODO Auto-generated method stub  
    7.         super.onCreate(savedInstanceState);  
    8.         setContentView(R.layout.layout_for_b);  
    9.           
    10.         initView();  
    11.           
    12.         getAndShowSerialzableObeject();  
    13.     }  
    14.       
    15.     public void initView(){  
    16.         mTextView = (TextView)findViewById(R.id.b_textview);  
    17.     }  
    18.       
    19.     public void getAndShowSerialzableObeject(){  
    20.         Bundle extras = getIntent().getExtras();  
    21.         if (extras != null){  
    22.             SerializableBook book = (SerializableBook) extras.get(SerializableKey);  
    23.             mTextView.setText("Name:" + book.getName()+" "  
    24.                                 + "Author:" + book.getAuthor() + " "  
    25.                                 + "Pubdate:" + book.getPubdate() + " "  
    26.                                 + "Price:" + book.getPrice());  
    27.         }else{  
    28.             mTextView.setText("nothing");  
    29.         }  
    30.     }  
    31. }  
  • 相关阅读:
    linux下ping命令出现ping: sendto: Network is unreachable
    tiny4412--linux驱动学习(2)
    tiny4412--linux驱动学习(1)
    linux-kernel-4.4 移植 (2)解决上部遗留DMA-PL330的问题
    linux-kernel-4.4 移植 (1)启动
    Busybox构建根文件系统和制作Ramdisk
    tiny4412 --Uboot移植(6) SD卡驱动,启动内核
    select响应事件
    项目总结1
    一个盒子只是显示两行
  • 原文地址:https://www.cnblogs.com/wangfeng520/p/5044654.html
Copyright © 2020-2023  润新知