Android---intent传递putStringArrayListExtra

Intent是Activity与Activity之间,Activity与Service之间传递参数的介质,使用Intent和Bundle在组件之间传递数据,而这两种通常实现的是Java基本对象类型和String的传递。
在实际项目中,页面之间传值,除了以上几种,经常还有传递Object对象、List类型、List<Object>类型和全局变量等等的需求。

一、传递List<String>和List<Integer>
以下以传递List<String>为例,发送List<String>语法为:
intent.putStringArrayListExtra(key, list);
接收List<String>的语法为:
list = (ArrayList<String>)getIntent().getStringArrayListExtra(key);
以下是一个运用实例:


ArrayList<String> stringList = new ArrayList<String>(); 
stringList.add("string1"); 
stringList.add("string2"); 
stringList.add("string3"); 
Intent intent = new Intent(); 
intent.setClass(ListDemoActivity.this, StringListActivity.class); 
intent.putStringArrayListExtra("ListString", stringList); 
startActivity(intent); 
接收
ArrayList<String> stringList = (ArrayList<String>) getIntent().getStringArrayListExtra("ListString"); 
 

List<Integer>类似以上的操作调用下面的方法也可以实现发送和接收:
intent.putIntegerArrayListExtra(key, list);
list =(ArrayList<Integer>) getIntent().getIntegerArrayListExtra(key);


二、使用Serializable和Parcelable两种方式传递Object
Android的Intent之间传递对象有两 种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putParcelable(Key,Object)。方法中的Object要满足一定的条件,前者实现了Serializable接口, 而后者实现了Parcelable接口。
以下是实现了Serializable接口的User类,命名为SerializableUser纯粹是从类名方便区分实现了Parcelable接口的User类:

public class SerializableUser implements Serializable { 
private String userName; 
private String password; 
public SerializableUser() { 

public SerializableUser(String userName, String password) { 
this.userName = userName; 
this.password = password; 

public String getUserName() { 
return userName; 

public void setUserName(String userName) { 
this.userName = userName; 

public String getPassword() { 
return password; 

public void setPassword(String password) { 
this.password = password; 


 


以下是实现了Parcelable接口的User类:

public class ParcelableUser implements Parcelable { 
private String userName; 
private String password; 
public ParcelableUser() { 

public ParcelableUser(String userName, String password) { 
this.userName = userName; 
this.password = password; 

public String getUserName() { 
return userName; 

public void setUserName(String userName) { 
this.userName = userName; 

public String getPassword() { 
return password; 

public void setPassword(String password) { 
this.password = password; 

public static final Parcelable.Creator<ParcelableUser> CREATOR = new Creator<ParcelableUser>() { 
@Override 
public ParcelableUser createFromParcel(Parcel source) { 
ParcelableUser parcelableUser = new ParcelableUser(); 
parcelableUser.userName = source.readString(); 
parcelableUser.password = source.readString(); 
return parcelableUser; 

@Override 
public ParcelableUser[] newArray(int size) { 
return new ParcelableUser[size]; 

}; 
@Override 
public int describeContents() { 
// TODO Auto-generated method stub 
return 0; 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
// TODO Auto-generated method stub 
dest.writeString(userName); 
dest.writeString(password); 


 


使用两种方式传递的语法分别为:
bundle.putSerializable(key,object);
bundle.putParcelable(key,object);
使用两种方式接收的语法分别为:
object=(Object) getIntent().getSerializableExtra(key);
object=(Object) getIntent().getParcelableExtra(key);

分别使用Serializable和Parcelable发送Object 
SerializableUser serializableUser = new SerializableUser("user1", "123456"); 
ParcelableUser parcelableUser = new ParcelableUser("user2","654321"); 
Intent intent = new Intent(); 
Bundle bundle = new Bundle(); 
bundle.putSerializable("serializableUser", serializableUser); 
bundle.putParcelable("parcelableUser", parcelableUser); 
intent.setClass(ListDemoActivity.this,ObjectActivity.class); 
intent.putExtras(bundle); 
startActivity(intent); 
接收Object
SerializableUser serializableUser = (SerializableUser) getIntent().getSerializableExtra("serializableUser"); 
ParcelableUser parcelableUser = (ParcelableUser) getIntent().getParcelableExtra("parcelableUser"); 
 


实现Serializable接口就是把对象序列化,然后再传输,和Java的常用编程没什么明显区别,而且User不需要明显改变,比较简单。
然而,后一种实现Parcelable接口的类比较复杂
Android提供了一种新的类型:Parcel,被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。
实现Parcelable接口需要实现三个方法:
)writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中。
声明:writeToParcel (Parcel dest, int flags)。
)describeContents方法。直接返回0就可以。
)静态的Parcelable.Creator<T>接口,本接口有两个方法:

createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。
通 过log测试输出可知程序的运行情况,在bundle.putParcelable(“parcelableUser”, parcelableUser);时,调用了ParcelableUser类中的publicvoid writeToParcel(Parcel dest, int flags)方法,并向dest写数据,在 ParcelableUserparcelableUser= (ParcelableUser)getIntent().getParcelableExtra(“parcelableUser”);的时候,调用了 ParcelableUser类中的public ParcelableUsercreateFromParcel(Parcel source) 方法,创建了一个ParcelableUser对象,并给这个对象的属性赋值,这里的Parcel source和Parcel dest是相同的,然后返回这个ParcelableUser对象。最后就可以打印出parcelableUser的属性信息了。

三、传递List<Object>
如果我们要传递的是Object组成的List列表,即List<Object>,首先需要将Object对象实现Serializable接口,然后把list强制类型转换成Serializable类型,最后通过:
Intent.putExtra(key, (Serializable)objectList);
这样的语法来传递,接收方在接收的时候也需要强制类型转换成List<Object>,接收 List<Object>使用的语法是:
objectList= (List<Object>) getIntent().getSerializableExtra(key);
以下是一个应用实例,这里使用的SerializableUser类在上一步有给出,这里就不再重复给出。

发送List<Object>
SerializableUser user1 = new SerializableUser("user1", "123456"); 
SerializableUser user2 = new SerializableUser("user2", "654321"); 
List<SerializableUser> objectList = new ArrayList<SerializableUser>(); 
objectList.add(user1); 
objectList.add(user2); 
Intent intent = new Intent(); 
intent.setClass(ListDemoActivity.this, ObjectListActivity.class); 
intent.putExtra("ListObject", (Serializable) objectList); 
startActivity(intent); 
接收List<Object>
List<SerializableUser> objectList = (List<SerializableUser>) getIntent().getSerializableExtra("ListObject"); 
 

四、全局变量
如果一些特殊的应用级别的参数,不方便使用intent来传递参数,我们很容易想到是不是有全局变量或静态变量可以使用?Java中的静态变量在这里是适合的,但其值在Activity调用了System.exit(0)或finish()后就丢失了。
而在android中有个更优雅的方式是使用ApplicationContext。这种全局变量方法相对静态类更有保障,直到应用的所有Activity全部被destory掉之后才会被释放掉。
Android 的SDK中有说道,Application是用来保存全局变量的,并且是在package创建的时候就存在了。所以当我们需要创建全局变量的时候,不需要 再像J2SE那样需要创建public权限的static变量,而直接在application中去实现。只需要调用Context的 getApplicationContext或者Activity的getApplication方法来获得一个Application对象,就可以设置 或读取全局变量的值。
启动Application时,系统会创建一个PID,即进程ID,所有的Activity就会在此进程上运行。那么我们在 Application创建的时候初始化全局变量,同一个应用的所有Activity都可以取到这些全局变量的值,换句话说,我们在某一个 Activity中改变了这些全局变量的值,那么在同一个应用的其他Activity中值就会改变。
用法:
. 创建一个属于你自己的android.app.Application的子类,为想要共享的private全局变量增加setter和getter方法。

public class MyApp extends Application{ 
private String globalVariable; 
public String getGlobalVariable() { 
return globalVariable; 

public void setGlobalVariable(String globalVariable) { 
this.globalVariable = globalVariable; 



. 在manifest中申明一下这个类,这时Android就为此建立一个全局可用的实例。
其实就是在原来仅有的一个application标签上为application制定一个名字为这个全局实例。

<application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name"></span> 
. 可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

使用全局变量传递参数 
MyApp myApp = ((MyApp) getApplicationContext());//获得我们的应用程序MyApp 
myApp.setGlobalVariable("全局变量"); 
Intent intent = new Intent(); 
intent.setClass(ListDemoActivity.this, GlobalActivity.class); 
startActivity(intent); 
接收全局变量的参数
MyApp myApp = ((MyApp) getApplicationContext()); 
String globalVariable = myApp.getGlobalVariable();

Android—Bundle传递ArrayList<T>

Android开发中Activity传值特别普遍,最贱开发需要传递集合List到另一个Activity,在此作出总结。

首先创建自己的实体类:我的暂命名为Gate。

声明List集合时候泛型中是你声明的实体类:List<Gate> gates=new ArrayList<Gate>;

我们要做的是将这个gates传递到要跳转的Activity,用到的方法是bundle.putParcelableArrayList("gates", gates)

如果你的尸体类只是声明变量以及添加对应的构造函数和set get方法,直接用以上方法传递集合会报错的,因为bundle.putParcelableArrayList("gates", gates)这个方法要求是集合中的泛型必须实现Parcelable接口;

所以我们的实体类应该如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

package com.example.galleryapp.domain;

import android.os.Parcel;

import android.os.Parcelable;

public class Gate implements Parcelable{

    private String id;

    private String name;

    public String getId() {

        return id;

    }

    public void setId(String id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Gate(String id, String name) {

        super();

        this.id = id;

        this.name = name;

    }

    public Gate() {

        super();

    }

    @Override

    public String toString() {

        return "Gate [id=" + id + ", name=" + name + "]";

    }

    @Override

    public int describeContents() {

        // TODO Auto-generated method stub

        return 0;

    }

    @Override

    public void writeToParcel(Parcel arg0, int arg1) {

        // TODO Auto-generated method stub

          // 1.必须按成员变量声明的顺序封装数据,不然会出现获取数据出错 

        // 2.序列化对象 

        arg0.writeString(id); 

        arg0.writeString(name);

    }

     

     // 1.必须实现Parcelable.Creator接口,否则在获取Person数据的时候,会报错,如下: 

    // android.os.BadParcelableException: 

    // Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.um.demo.Person 

    // 2.这个接口实现了从Percel容器读取Person数据,并返回Person对象给逻辑层使用 

    // 3.实现Parcelable.Creator接口对象名必须为CREATOR,不如同样会报错上面所提到的错; 

    // 4.在读取Parcel容器里的数据事,必须按成员变量声明的顺序读取数据,不然会出现获取数据出错 

    // 5.反序列化对象 

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

   

        @Override 

        public Gate createFromParcel(Parcel source) { 

            // TODO Auto-generated method stub 

            // 必须按成员变量声明的顺序读取数据,不然会出现获取数据出错 

            Gate p = new Gate(); 

            p.setId(source.readString()); 

            p.setName(source.readString()); 

            return p; 

        

   

        @Override 

        public Gate[] newArray(int size) { 

            // TODO Auto-generated method stub 

            return new Gate[size]; 

        

    }; 

}

 以上的重点是实现了Parcelable接口,并且重写了其中的方法。

重点代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

@Override

    public void writeToParcel(Parcel arg0, int arg1) {

        // TODO Auto-generated method stub

         // 1.必须按成员变量声明的顺序封装数据,不然会出现获取数据出错 

        // 2.序列化对象 

        arg0.writeString(id); 

        arg0.writeString(name);

    }

 // 1.必须实现Parcelable.Creator接口,否则在获取Person数据的时候,会报错,如下: 

    // android.os.BadParcelableException: 

    // Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.um.demo.Person 

    // 2.这个接口实现了从Percel容器读取Person数据,并返回Person对象给逻辑层使用 

    // 3.实现Parcelable.Creator接口对象名必须为CREATOR,不如同样会报错上面所提到的错; 

    // 4.在读取Parcel容器里的数据事,必须按成员变量声明的顺序读取数据,不然会出现获取数据出错 

    // 5.反序列化对象 

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

  

        @Override 

        public Gate createFromParcel(Parcel source) { 

            // TODO Auto-generated method stub 

            // 必须按成员变量声明的顺序读取数据,不然会出现获取数据出错 

            Gate p = new Gate(); 

            p.setId(source.readString()); 

            p.setName(source.readString()); 

            return p; 

        

  

        @Override 

        public Gate[] newArray(int size) { 

            // TODO Auto-generated method stub 

            return new Gate[size]; 

        

    };

详细请阅读注释,如果没有以上两端代码,传值一样会失败。

传值:

1

2

3

4

5

6

Intent intent = new Intent(LoginActivity.this,

                        PeizhiActivity.class);

                Bundle bundle = new Bundle();

                bundle.putParcelableArrayList("gates", gates);

                intent.putExtras(bundle);

                startActivity(intent);

 接收:

1

2

Bundle bundle = getIntent().getExtras();

        gates = bundle.getParcelableArrayList("gates");

猜你喜欢

转载自blog.csdn.net/weixin_38451161/article/details/85259651
今日推荐