A sentimental robot

Transmission 본문

Android Studio

Transmission

GOD03219 2018. 1. 31. 14:49

// MainActivity.java


public void f1(View view) {


Intent intent=new Intent(getApplicationContext(),AppleActivity.class);



intent.putExtra("text3",new Tiger(10,"sora")); // 객체 넘기기

 // 배열 넘기기

int ar[]=new int[]{1,2,3,4,5};
intent.putExtra("text4",ar);

 

String str[]=new String[]{"so","ra"};
intent.putExtra("text5",str);
Tiger []ta2=new Tiger[]{
new Tiger(100,"개나리"),
new Tiger(200,"민들레"),
new Tiger(300,"버섯"),
new Tiger(400,"나무")
};
intent.putExtra("text6",ta2);
// Collection
ArrayList<Integer>li=new ArrayList<>();
li.add(10);
li.add(20);
li.add(30);
li.add(40);
intent.putExtra("text7",li);

ArrayList<Tiger>lio=new ArrayList<>();
lio.add(new Tiger(1,"eun"));
lio.add(new Tiger(2,"bi"));
intent.putExtra("text9",lio);

 

startActivity(intent);



}


}

// Tiger.java

package com.example.i310_54.myapplication01;

import java.io.Serializable;

/**
* Created by I310-54 on 2018-01-31.
*/

public class Tiger implements Serializable{
int n;
String s;

public Tiger(int n, String s) {
this.n = n;
this.s = s;
}

public int getN() {
return n;
}

public void setN(int n) {
this.n = n;
}

public String getS() {
return s;
}

public void setS(String s) {
this.s = s;
}
}

// AppleActivity.java

 

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);

Intent intent=getIntent();


Tiger c=(Tiger)intent.getSerializableExtra("text3");



Log.v("debug",c.getS());

int ar[]=intent.getIntArrayExtra("text4");

// int ar[]=(int [])intent.getSerializableExtra("text4"); 가능하다.


for(int a:ar){

Log.v("debug"," "+a);
}
String br[]=intent.getStringArrayExtra("text5");
for(String date:br){
Log.v("debug",date);
}
Tiger [] e =(Tiger[]) intent.getSerializableExtra("text6");
for(Tiger each:e){

Log.v("debug",each.getS());
}
ArrayList<Integer> li=(ArrayList<Integer>) intent.getSerializableExtra("text7");


for(Integer a:li){

Log.v("debug",""+a);
}
ArrayList<Tiger>lio=(ArrayList<Tiger>) intent.getSerializableExtra("text9");

for(Tiger ti:lio){

Log.v("debug2",""+ti.getN());
}

}

 

'Android Studio' 카테고리의 다른 글

배경색 자동으로 바꾸기  (0) 2018.02.01
TableLayout  (0) 2018.02.01
Activity  (0) 2018.01.31
Activity transition/intent  (0) 2018.01.30
ListView  (0) 2018.01.29