Bundle 로 Object, ArrayList 넘기기 예제 입니다.
Intent를 사용하실 경우에는 아래와 같이 사용하며
1 2 3 4 5 6 7 8 9 10 11 | // 줄때 Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putSerializable( "Obj" , item); // Object 넘기기 bundle.putParcelableArrayList( "list" , (ArrayList<? extends Parcelable>) list); // list 넘기기 intent.putExtras(bundle); // 받을때 Bundle bundle = getIntent().getExtras(); bundle.getSerializable( "Obj" ); bundle.getParcelableArrayList( "list" );<p></p> |
Fragment를 통한 사용은 아래와 같이 사용하시면 됩니다.
1 2 3 4 5 6 7 8 9 10 11 | // 줄때 Fragment f = new Fragment(); Bundle bundle = new Bundle(); bundle.putSerializable( "Obj" , item); // Object 넘기기 bundle.putParcelableArrayList( "list" , (ArrayList<? extends Parcelable>) list); // list 넘기기 f.setArguments(bundle); // 받을때 Bundle bundle = getArguments(); bundle.getSerializable( "Obj" ); bundle.getParcelableArrayList( "list" ); |
Java 사용 예제
(Serializable)Item 클래스의 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.io.Serializable; public class Item implements Serializable { public int index; public String address; // 주소 public String title; // 제목 public Item( int index, String address, String title) { this .index = index; this .address = address; this .title = title; } } |
(Parcelable)Item2 클래스의 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import android.os.Parcel; import android.os.Parcelable; public class Item2 implements Parcelable { public int index; public String address; // 주소 public String title; // 제목 public Item2( int index, String address, String title) { this .index = index; this .address = address; this .title = title; } public Item2(Parcel in) { index = in.readInt(); address = in.readString(); title = in.readString(); } public static final Creator<Item2> CREATOR = new Creator<Item2>() { @Override public Item2 createFromParcel(Parcel in) { return new Item2(in); } @Override public Item2[] newArray( int size) { return new Item2[size]; } }; @Override public int describeContents() { return 0 ; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(index); dest.writeString(address); dest.writeString(title); } } |
보내는 부분의 Java 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Fragment f = new Fragment(); Bundle bundle = new Bundle(); bundle.putInt( "id" , 1122 ); Item item = new Item( 123 , "abc" , "가나다" ); // Object 넘기기 bundle.putSerializable( "item" , item); List<Item2> list = new ArrayList<>(); list.add( new Item2( 222 , "ddd" , "다다다" )); list.add( new Item2( 223 , "eee" , "다다다" )); list.add( new Item2( 224 , "ddd" , "다다다" )); // 리스트 넘기기 bundle.putParcelableArrayList( "list" , (ArrayList<? extends Parcelable>) list); f.setArguments(bundle); |
받는 부분의 Java 코드입니다.
1 2 3 4 5 6 7 8 9 10 | Bundle bundle = getArguments(); int id = bundle.getInt( "id" ); Item item = (Item) bundle.getSerializable( "item" ); List<Item2> item2 = bundle.getParcelableArrayList( "list" ); Log.v( "test" , "id : " +id); Log.w( "test" , "item : " +item.index+ " , " +item.address+ " , " +item.title); Log.d( "test" , "item2 : " +item2.size()); for (Item2 it : item2) Log.i( "test" , "it : " +it.index+ " , " +it.address+ " , " +it.title); |
'Android' 카테고리의 다른 글
[안드로이드] tts(TextToSpeech) example (0) | 2017.04.22 |
---|---|
[안드로이드] android google map cluster example (0) | 2017.04.18 |
[안드로이드] sqlite Like 검색 예제 (0) | 2017.04.17 |
[안드로이드] arraylist 초성 검색 예제 (0) | 2017.04.17 |
[안드로이드] System.currentTimeMillis() 에서 몇분전, 몇시간전, 몇일전 등등 계산하기 (4) | 2017.04.16 |