Android怎样实现传递对象集合?

我们先来看看效果图

                                       

布局代码的实现1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_test1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical"
    tools:context="com.example.android12.Test1Activity">
   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="传递对象集合"
      android:onClick="yes"
       />
</LinearLayout>

布局代码的实现2
<?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:id="@+id/activity_receive"
    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="com.example.android12.ReceiveActivity">
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/tv_test1_show"
       />
</RelativeLayout>


Java代码实现1

package com.example.android12;

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

import com.example.entity.Person;

import java.util.ArrayList;

public class Test1Activity extends AppCompatActivity {

    private TextView tv_test1_objects;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);


    }
    public void yes(View view){
        //跳转到哪个页面
        Intent intent=new Intent(this,ReceiveActivity.class);
        //传递对象
        Person p1=new Person(1,19,"张三");
        Person p2=new Person(2,19,"李四");
        Person p3=new Person(3,19,"王麻子");
        //放入集合中
        ArrayList<Person> persons=new ArrayList<>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);
        intent.putParcelableArrayListExtra("persons",persons);
       startActivityForResult(intent,0x201);
    }

}

Java代码实现2

package com.example.android12;

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

import com.example.entity.Person;

import java.util.ArrayList;
import java.util.List;

public class ReceiveActivity extends AppCompatActivity {

    private TextView tv_test1_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        tv_test1_show = (TextView) findViewById(R.id.tv_test1_show);
        //接收对象集合
        List<Person> persons=getIntent().getParcelableArrayListExtra("persons");
        //根据个人喜好设置字体大小
        tv_test1_show.setTextSize(30);
        for (Person person:persons){
            tv_test1_show.setText(tv_test1_show.getText()+"\n"+person.toString());
        }
    }
}

实体类

package com.example.entity;

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

import java.io.Serializable;

/**
 * Created by Administrator on 2017/6/16 0016.
 */

/**
 * 若想传递对象集合不能使用Java的序列化(Serializable),只能使用Android的序列化
 * Java的序列化只能传递一个对象不能传递一个对象集合;但是Android的序列化既能传递对象也能传递对象集合
 */
public class Person implements Parcelable {
     private int pid;
    private String pname;
    private int page;

    public Person() {
    }

    public Person(int pid, int page, String pname) {
        this.pid = pid;
        this.page = page;
        this.pname = pname;
    }

    protected Person(Parcel in) {
        pid = in.readInt();
        pname = in.readString();
        page = in.readInt();
    }

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

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

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    @Override
    public String toString() {
        return "Person{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", page=" + page +
                '}';
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(pid);
        dest.writeString(pname);
        dest.writeInt(page);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值