Android中使用Intent传递Object和ArrayList<Object>对象和获取

来源:互联网 发布:淘宝大学vip课有用么 编辑:程序博客网 时间:2024/06/06 13:07

序列化的两种方式,使用Serializable接口和parcelable

serializable只需要bean类继承serializable接口,而parcelable重写Parcelable接口中的两个方法和静态变量CREATOR.


传递一些基本类型数据的方法如下:

  • putExtra(String name, int value)
  • putExtra(String name, String value)
  • putExtra(String name, float value)
  • putExtra(String name, double value)
  • putExtra(String name, long value)
  • putExtra(String name, boolean value)
  • putExtra(String name, byte value)
  • putExtra(String name, char value)

传递基本类型ArrayList对象的方法如下:

  • putIntegerArrayListExtra(String name, ArrayList《Integer》 value)
  • putStringArrayListExtra(String name, ArrayList《Integer》 value)

但是我们并没有发现直接传递Object类型的方法,但是在开发中经常会需要传递一个Object类型或者ArrayList《Object》类型的数据,那么我们应该怎样实现呢? 
方法如下:

传递Object类型:

  1. putExtra(String name, Serializable value)
  2. putExtra(String name, Parcelable value)

传递ArrayList《Object》类型:

  1. putExtra(String name, Serializable value)
  2. putExtra(String name, Parcelable value)
  3. putParcelableArrayListExtra(String name, ArrayList《? extends Parcelable》 value)


下面我们将student对象从Activity传递给Service。

Student类

package com.example.intenttest;public class Student implements Serializable{    private String name;    private String sex;    private int id;    public Student(String name, String sex, int id) {        super();        this.name = name;        this.sex = sex;        this.id = id;    }    @Override    public String toString() {        return "Student [name=" + name + ", sex=" + sex + ", id=" + id + "]";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }}
  • 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

Activity类:

package com.example.intenttest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 创建Student对象        Student student = new Student("小灰灰", "男", 1);        // 通过Intent传递对象给Service        Intent intent = new Intent(MainActivity.this, MyService.class);        intent.setAction("action");        intent.putExtra("student", student);        startService(intent);    }}
  • 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

Service类:

package com.example.intenttest;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service{    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        if("action".equals(intent.getAction())){            Student student = (Student) intent.getSerializableExtra("student");            Log.i("tag", "学生对象的toString():"+student);        }        return super.onStartCommand(intent, flags, startId);    }}
  • 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

结果: 
这里写图片描述



将ArrayList《Object》对象从MainActivity传递给SecondActivity: 
MainActivity类:

package com.example.intenttest;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 这里必须为ArrayList,不能为List        ArrayList<Student> list = new ArrayList<Student>();        // 创建Student对象        Student student1 = new Student("小灰灰", "男", 1);        Student student2 = new Student("大灰狼", "男", 2);        Student student3 = new Student("红太狼", "女", 3);        list.add(student1);        list.add(student2);        list.add(student3);        // 通过Intent传递对象给Service        Intent intent = new Intent(MainActivity.this, SecondActivity.class);        intent.setAction("action");        intent.putExtra("studentlist", list);        startActivity(intent);    }}
  • 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

(注意:MainActivity类中在声明list的时候必须用ArrayList而不能用List,否则就会出现如图所示的提示错误信息, 
这里写图片描述 

SecondActivity类:

package com.example.intenttest;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;public class SecondActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = getIntent();        if ("action".equals(intent.getAction())) {            ArrayList<Student> list = (ArrayList<Student>) intent.getSerializableExtra("studentlist");            for(int i = 0;i<list.size();i++){            Log.i("tag", "student:" + list.get(i));            }        }    }}
  • 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

运行结果: 
这里写图片描述



以上是使用putExtra(String name, Serializable value)方法来进行Object和ArrayList《Object》的传递的代码。 
通过 putExtra(String name, Parcelable value)和putParcelableArrayListExtra(String name, ArrayList《? extends Parcelable》 value)两个方法与putExtra(String name, Serializable value)的使用方法相同,只不过是序列化的方式不同。

0 0
原创粉丝点击