android Ativity之间传值一般使用Intent这个类
一、单行数据
传值
Intent intent=new Intent(this,ThinkActivity.class);
//传递基本数据类型
//intent.putExtra("uname",传递的数据);
拿值
String uname=getIntent().getStringExtra("uname");
二、多行数据
这里我们需要使用Bundle
传值
//传递Bundle
Intent intent=new Intent(this,ThinkActivity.class);
Bundle bundle=new Bundle(); bundle.putString("uname",数据); bundle.putInt("uage",数据);
intent.putExtra("bundle",bundle);
拿值
//接受bundle
Bundle bundle=getIntent().getBundleExtra("bundle");
String uname=bundle.getString("uname");
int uage=bundle.getInt("uage");
三、传对象(需要将对象进行序例化)
传值
Intent intent=new Intent(this,ThinkActivity.class);
Person person=new Person(1,"静静",19);
intent.putExtra("persons",persons);
拿值
Person person= (Person) getIntent().getSerializableExtra("person");
Person person=getIntent().getParcelableExtra("person");
四、传对象集合(需要使用Android的方法将对象进行实例化)
传值
Intent intent=new Intent(this,ThinkActivity.class);
Person person1=new Person(1,"静静",19);
Person person2=new Person(2,"明明",19);
Person person3=new Person(3,"雨田君",19);
ArrayList<Person> persons=new ArrayList<>();
persons.add(person1);
persons.add(person2);
persons.add(person3);
//传递对象集合
intent.putParcelableArrayListExtra("persons",persons);
拿值
List<Person> persons=getIntent().getParcelableArrayListExtra("persons");
然后循环遍历