Android uses putExtra to transmit data. Android putextra

Source: Internet
Author: User

Android uses putExtra to transmit data. Android putextra

When data is transmitted through intent, use the following code to report an error:

hMap<String, Object> map=(Map<String, Object>) parent.getItemAtPosition(position);intent.putExtra("userInfoMap", map);

However, the following code is normal:

HashMap<String, Object> map=(HashMap<String, Object>) parent.getItemAtPosition(position);intent.putExtra("userInfoMap", map);

Why is this? I searched for it on the Internet and found that only data that can be serialized can be put in. There are several methods for intent to transmit data:

Intent putExtra(String name, String[] value)Intent putExtra(String name, Parcelable value)Intent putExtra(String name, long value)Intent putExtra(String name, boolean value)Intent putExtra(String name, double value)Intent putExtra(String name, Parcelable[] value)Intent putExtra(String name, char value)Intent putExtra(String name, int[] value)Intent putExtra(String name, int value)Intent putExtra(String name, double[] value)   Intent putExtra(String name, short value)     Intent putExtra(String name, long[] value)     Intent putExtra(String name, boolean[] value)     Intent putExtra(String name, short[] value)     Intent putExtra(String name, String value)     Intent putExtra(String name, Serializable value)     Intent putExtra(String name, float[] value)     Intent putExtra(String name, Bundle value)     Intent putExtra(String name, byte[] value)     Intent putExtra(String name, CharSequence value)     Intent putExtra(String name, char[] value)     Intent putExtra(String name, byte value)     Intent putExtras(Intent src)     Intent putExtras(Bundle extras)
We can see that it can pass basic data types (including arrays of basic data types), String (including arrays), and Parcelable (including arrays), Serializable, Bundle, CharSequence, Intent data types. For data of Map type (superclass) and Long, Integer, Double, Byte, Float, Short, because the serialization interface is not implemented, the value cannot be passed through this method.

The following method can be used to transmit serialized data:

Submit activity

 lst.setOnItemClickListener(new OnItemClickListener() {        public void onItemClick(AdapterView<?> parent, View view,      int position, long id) {          HashMap map=(HashMap) parent.getItemAtPosition(position);     Intent intent=new Intent();     intent.setClass(btsAllinfoActivity.this,btsMapActivity.class);     intent.putExtra("bts_map", map);            startActivity(intent);         }   });
 
Receive data activity
Intent intent=getIntent();  map=(HashMap)intent.getSerializableExtra("bts_map");    System.out.println("111111111111");  System.out.println(map.get("bt_id"));  System.out.println(map.get("bt_name"));  System.out.println(map.get("bt_lon"));  System.out.println(map.get("bt_lat"));  System.out.println("111111111111");
You can use the following method for Bundle data:

Submit activity

 HashMap map=(HashMap) parent.getItemAtPosition(position);     Intent intent=new Intent();     intent.setClass(btsAllinfoActivity.this,btsMapActivity.class);     Bundle bundle = new Bundle();        bundle.putSerializable("bts_map", map);     intent.putExtras(bundle);          startActivity(intent);


 
Receive activity
Bundle bundle = getIntent().getExtras();    map=(HashMap)bundle.getSerializable("bts_map");    System.out.println("111111111111");  System.out.println(map.get("bt_id"));  System.out.println(map.get("bt_name"));  System.out.println(map.get("bt_lon"));  System.out.println(map.get("bt_lat"));  System.out.println("111111111111");

For Parcelable data

For Android, the transmission of complex types is mainly to convert their classes into basic byte arrays, and data passing between activities is implemented through Intent.

Android serialization objects can be implemented in two ways: Serializable interface or Parcelable interface.

The implementation of the Serializable interface is supported by JavaSE, and Parcelable is a feature specific to Android, which is more efficient than the implementation of the Serializable interface,

It can also be used in inter-process communication (IPC. The implementation of the Serializable interface is very simple. Just declare it.

The implementation of the Parcelable interface is a little more complex, but more efficient. We recommend that you use this method to improve performance.
Role of the Parcelable interface: an instance that implements the Parcelable interface can write its own state information (the State information usually refers to the value of each member variable) into Parcel,

You can also restore the status from Parcel. Parcel is used to complete data serialization and transmission.

You can use the following method to transmit Parcel data:

Step 1: implement the Parcelable interface to serialize objects. 2. Implement the public void writeToParcel (Parcel dest, int flags) method of the Parcelable interface. 3. The custom type must contain a static member named CREATOR. The member object must implement the Parcelable. Creator interface and its method.
Object Class implementation interface:

public class Person implements Parcelable {     public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {         @Override        public Person[] newArray(int size) {            return null;        }         @Override        public Person createFromParcel(Parcel source) {            Person result = new Person();            result.age = source.readInt();            result.name = source.readString();            result.address = source.readParcelable(Address.class.getClassLoader());            return result;        }    };    private Address address;    private String name;    private int age;     @Override    public int describeContents() {        return 0;    }     public Address getAddress() {        return address;    }     public void setAddress(Address address) {        this.address = address;    }     @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeInt(age);        dest.writeString(name);        dest.writeParcelable(address, PARCELABLE_WRITE_RETURN_VALUE);    }     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;    }     @Override    public String toString() {        return "Person [address=" + address + ", name=" + name + ", age=" + age                + "]";    } }

// The official document says that Parce is used between different processes and Bind, public void sendBroadCast (View viwe) {Log. d ("test", Thread. currentThread (). getName () + "sent a message to broadcast"); Person person = new Person (); person. setAge (18); Address address = new Address (); address. setStreet ("Yanling South Road"); person. setName ("carlos"); person. setAddress (address); Intent intent = new Intent ("aaaa"); intent. setClass (this, SendActivity. class); // intent. putParcelableArrayListExtra (name, value) intent. putExtra ("person", person); startActivity (intent );}




Reference: http://xiaomi4980.blog.163.com/blog/static/21594519620141236363445/

Http://www.cnblogs.com/carlosk/archive/2012/09/28/2707521.html


[Android Development]: several data transmission methods between activities

1. intent is used to transmit data Intent tables. In many cases, Android Intent is used to transmit data between various activities. This is also an official data transfer method for Android. Requirement 1: jump from an Activity (IntentDemo) to another Activity (Other). The Demo of the Data Program passed by Intent is as follows: IntentDemo. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class IntentDemo extends Activity {private Button button; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initComponent (); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (IntentDemo. this, Other. class); // transmits data Intent in intent. putExtra ("name", "AHuier"); intent. putExtra ("age", 22); intent. putExtra ("address", "XiaMen"); // start Intent startActivity (intent) ;}};} private void initComponent () {button = (Button) findViewById (R. id. button) ;}} other. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class Other extends Activity {private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. other); initComponent (); Intent intent = getIntent (); int age = intent. getIntExtra (...... remaining full text>

Android development: How to Use putExtra and getStringExtra for data transmission?

In onCreate of Mission31, use getIntent (). getExtras ();

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.