Intent传递Parcelable List对象

步骤:

1.首先对象要实现Parcelable接口

2.用Intent发送对象或者list,关键代码

bundle.putParcelable("student", stu);

bundle.putParcelableArrayList("student_list", list);


3.用Intent获取对象或者list ,关键代码 

Student student = (Student) intent.getExtras().get("student");

ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");


下面给出完整代码示例:

1. Strudent.java 实现Parcelable接口

package cn.getchance.testparcelable;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by chengyi on 15/11/5.
 */
public class Student implements Parcelable {


    private String name;
    private int age;

    protected Student() {
    }

    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;
    }

    protected Student(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
}

2.MainActivity.java 跳转并传递List和Object数据

package cn.getchance.testparcelable;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student stu = new Student();
                stu.setAge(108);
                stu.setName("s1");

                Student stu2 = new Student();
                stu2.setAge(109);
                stu2.setName("s2");


                Student stu3 = new Student();
                stu3.setAge(110);
                stu3.setName("s3");
                ArrayList<Student> list = new ArrayList<Student>();
                list.add(stu);
                list.add(stu2);
                list.add(stu3);

                Intent i = new Intent(MainActivity.this, StudentActivity.class);
                Bundle bundle = new Bundle();

                //传递对象
                bundle.putParcelable("student", stu);

                //传递List ,这里注意只能传ArrayList
                bundle.putParcelableArrayList("student_list", list);
                i.putExtras(bundle);
                MainActivity.this.startActivity(i);
            }
        });
    }
}


3.StudentActivity.java 接收MainActivity传递过来的List的数据

package cn.getchance.testparcelable;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.util.ArrayList;

public class StudentActivity extends AppCompatActivity {

    private TextView tv, tv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        tv = (TextView) findViewById(R.id.tv);
        tv2 = (TextView) findViewById(R.id.tv2);
        Intent intent = getIntent();
        if (intent != null) {
            Bundle b = intent.getExtras();
            Student student = (Student) b.get("student");
            if (student != null) {
                tv.setText(student.getName() + ":" + student.getAge());
            }

            //关键性代码,通过intent.getParcelableArrayListExtra方法获取list数据
            ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");
            if (list != null && list.size() > 0) {
                String str = "";
                for (Student s : list) {
                    str += s.getName() + " | ";
                }
                tv2.setText(str);
            }
        }
    }
}

activity_student.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.getchance.testparcelable.StudentActivity">


    <TextView
        android:id="@+id/tv"
        android:text="student"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv2"
        android:layout_below="@id/tv"
        android:layout_marginTop="20dp"
        android:text="student"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

activity_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.getchance.testparcelable.StudentActivity">


<TextView
    android:id="@+id/tv"
    android:text="student"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/tv2"
    android:layout_below="@id/tv"
    android:layout_marginTop="20dp"
    android:text="student"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.getchance.testparcelable">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".StudentActivity"></activity>
    </application>

</manifest>







  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值