Android实现两个活动间数据的传递

1、确定需要传送的数据;如果需要传送list数据,数据及对象声明应在一个全新的类中。

2、在传送数据的活动中使用intent:如果需要传送单独的一个数据则只需使用intent.putExtra(一句话或者一个数据)即可,如果需要传递一个list数据,则需传递list对象,则使用intent.putExtra("自定义的新对象名",活动中已声明的对象名)。

例:

Intent intent = new Intent();
intent.setClass(Pig_condition.this, change_pig_information.class);
intent.putExtra("pig", pig);
startActivity(intent);
finish();
注:使用intent.putExtra传送接收数据时需要实现Serializable接口的功能,否则不能传递数据且此句会报错,
实现这个接口的功能,只需要在这个传递的对象的类后implements Serializable即可。
3、在另一个活动中接收数据,实现Serializable接口功能:

    例:         

Intent intent=getIntent();
Pig pig=(Pig) intent.getSerializableExtra("pig");
4、实例:在两个Activity间传递pig对象的数据

pig.java

package com.example.entity;

import java.io.Serializable;

/**
 * Created by Administrator on 2018.3.16.
 */

public class Pig implements Serializable {
    private int prid;
    private int batch;
    private int  firstembryo;
    private int     secondembryo;
    private int  thirdembryo;
    private int  fourthembryo;
    private int    fifthembryo;
    private int      sixthembryo;
    private String      currentstate;
    private String    nextproduce;
    public String getCurrentstate(){return currentstate;}
    public String getNextproduce(){return nextproduce;}
    public int getPrid(){return prid;}
    public int getBatch(){return batch;}
    public int getFirstembryo(){return firstembryo;}
    public int getSecondembryo(){return secondembryo;}
    public   int getThirdembryo(){return thirdembryo;}
    public int getFourthembryo(){return fourthembryo;}
    public int getFifthembryo(){return fifthembryo;}
    public int getSixthembryo(){return sixthembryo;}
    public void setPrid(int prid){
        this.prid=prid;
    }
    public void setBatch(int batch){
        this.batch=batch;
    }
    public void setFirstembryo(int firstembryo){
        this.firstembryo=firstembryo;
    }

    public void setFourthembryo(int fourthembryo) {
        this.fourthembryo = fourthembryo;
    }

    public void setNextproduce(String nextproduce) {
        this.nextproduce = nextproduce;
    }

    public void setSecondembryo(int secondembryo) {
        this.secondembryo = secondembryo;
    }

    public void setSixthembryo(int sixthembryo) {
        this.sixthembryo = sixthembryo;
    }

    public void setThirdembryo(int thirdembryo) {
        this.thirdembryo = thirdembryo;
    }

    public void setCurrentstate(String currentstate) {
        this.currentstate = currentstate;
    }

    public void setFifthembryo(int fifthembryo) {
        this.fifthembryo = fifthembryo;
    }

    private int pid;
    private String position;
    private String temperature;
    private String weight;
    private String medical;
    private String month;

    public String getPosition(){
        return position;
    }

    public String getMedical(){
        return medical;
    }

    public String getMonth(){
        return month;
    }

    public int getPid(){
        return pid;
    }

    public String getTemperature(){
        return temperature;
    }

    public String getWeight(){
        return weight;
    }

    public void setMedical(String medical) {
        this.medical = medical;
    }

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

    public void setPosition(String position) {
        this.position = position;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }
}

传送数据的pig_condition:

pig.setPosition(json.getString("position"));
pig.setWeight(json.getString("weight"));
pig.setTemperature(json.getString("temperature"));
pig.setMedical(json.getString("medical"));
pig.setMonth(json.getString("month"));
pig.setSecondembryo(json.getInt("secondembryo"));
pig.setFirstembryo(json.getInt("firstembryo"));
pig.setThirdembryo(json.getInt("thirdembryo"));
pig.setFourthembryo(json.getInt("fourthembryo"));
pig.setFifthembryo(json.getInt("fifthembryo"));
pig.setSixthembryo(json.getInt("sixthembryo"));
pig.setCurrentstate(json.getString("currentstate"));
pig.setNextproduce(json.getString("nextproduce"));
pig.setBatch(json.getInt("batch"));
list.add(pig);
 
 
Intent intent = new Intent();
intent.setClass(Pig_condition.this, change_pig_information.class);
intent.putExtra("pig", pig);
startActivity(intent);
finish();
接收对象的change_pig_information:
Intent intent=getIntent();
Pig pig=(Pig) intent.getSerializableExtra("pig");
TextView pridView=(TextView)findViewById(R.id.prid_tView1);
TextView batchView=(TextView)findViewById(R.id.batch_tView1);
TextView pidView=(TextView)findViewById(R.id.pid_tView1);
TextView positionView=(TextView)findViewById(R.id.position_tView1);
TextView weightView=(TextView)findViewById(R.id.weight_tView1);
TextView tempView=(TextView)findViewById(R.id.temp_tView1);
TextView monthView=(TextView)findViewById(R.id.month_tView1);
TextView medicalView=(TextView)findViewById(R.id.medical_tView1);
TextView firstView=(TextView)findViewById(R.id.firstembryo_tView1);
TextView secondView=(TextView)findViewById(R.id.secondembryo_tView1);
TextView thirdView=(TextView)findViewById(R.id.thirdembryo_tView1);
TextView fourthView=(TextView)findViewById(R.id.fourthembryo_tView1);
TextView fifthView=(TextView)findViewById(R.id.fifthembryo_tView1);
TextView sixthView=(TextView)findViewById(R.id.sixthembryo_tView1);
TextView currentView=(TextView)findViewById(R.id.currentstate_tView1);
TextView nextproduceView=(TextView)findViewById(R.id.nextproduce_tView1);

pridView.setText("是否是母猪:"+pig.getPrid());
batchView.setText("产胎数:"+pig.getBatch());
pidView.setText("猪只标号:"+pig.getPid());
positionView.setText("栏位:"+pig.getPosition());
weightView.setText("体重:"+pig.getWeight()+"kg");
tempView.setText("体温:"+pig.getTemperature()+"℃");
monthView.setText("年龄:"+pig.getMonth());
medicalView.setText("病史:"+pig.getMedical());
firstView.setText("第一胎个数:"+pig.getFirstembryo());
secondView.setText("第二胎个数:"+pig.getSecondembryo());
thirdView.setText("第三胎个数:"+pig.getThirdembryo());
fourthView.setText("第四胎个数:"+pig.getFourthembryo());
fifthView.setText("第五胎个数:"+pig.getFifthembryo());
sixthView.setText("第六胎个数:"+pig.getSixthembryo());
currentView.setText("现是否怀孕:"+pig.getCurrentstate());
nextproduceView.setText("预产期:"+pig.getNextproduce());

 
 

猜你喜欢

转载自blog.csdn.net/fblacklister/article/details/80138137