Android uses bundles to transfer custom types

Source: Internet
Author: User

In Android development, it is sometimes necessary to pass data from one activity to another activity, where simple data types such as String, int, float, etc. are already encapsulated in the bundle. But what if we want to pass a complex data type, such as a book object?
Take a closer look at the method in the bundle, one of which is the Putserializable () method, the Serializable object is a recoverable object interface, we only need to let the book object implement Serializable interface, You can pass the book object using the Bundle.putserializable () method. Don't say much nonsense, now put the code on:
Book Class:

Package Com.bundletest.model.fneg;

Import java.io.serializable;/**

* @Copyright: Copyright (c) 2008–2100
* @Company: Sagret
* @Author: Fengcunhan [email protected]
* @Package: Com.bundletest.model.fneg
* @FileName: Book.java
* @Time: 2010-12-19
* @User: Feng
*/
public class book implements Serializable {
/**
*
*/
Private static final long serialversionuid = 1L;

private String name;
Private String ID;
Private String author;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String Getauthor () {
return author;
}
public void Setauthor (String author) {
This.author = author;
}

}

Instantiate the book class, get the book object book, and set the member variables:

if (Textutils.isempty (bookname) | | Textutils.isempty (author) | | Textutils.isempty (ID)) {
Toast.maketext (androidbundleactivity.this, "input box cannot be empty", Toast.length_short). Show ();
}else{
Book Book=new book ();
Book.setname (BookName);
Book.setauthor (author);
Book.setid (ID);
Intent intent=new Intent (androidbundleactivity.this,recieveactivity.class);
Bundle Bundle=new Bundle ();
Bundle.putserializable ("book", book);
Intent.putextras (bundle);
StartActivity (Intent);
}

Gets the book object passed over in another activity and displays:

Intent intent=this.getintent ();
Bundle Bundle=intent.getextras (); Book book=-bundle.getserializable ("book"); Nametext.settext ("title:" +book.getname ()); Authortext.settext ("" +book.getauthor ()); Idtext.settext ("ID:" +book.getid ());

Android uses bundles to transfer custom types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.