본문 바로가기

프로그래밍/Android

Parcelable

 

 

소포 꾸러미

 

자바 자체가 리플렉션과 같이 동적으로 클래스를 생성하고, 메쏘드들을 확장해 나갈 수 있는데

이러한 기능을 이용한 오브젝트 전달 방법이다.

소포에는 실제 데이터들만이 넣어지고, 전달되는데 받는 쪽에서는 이것을 풀어

원래의 객체에 넣어 주어야 한다.

 

원래의 객체를 생성하기 위해 꾸러미를 작성할때 클래스의 정보가 포함되고,

수신측에서 약속된 전역 멤버를 호출하여 객체를 생성하게 된다.

 

실제 구조는 확인하지 않았지만 아마도 클래스 로드 하고, 메쏘드 가져와 invoke() .. 

이런 구조가 아닐까?

 

 

안드로이드에서 Intent로 데이터를 전송할때, parcelable을 사용하면

Object들을 함께 전달 할 수 있다. 

 

Parcelable을 상속받고, 메쏘드와 CREATOR라는 전역 변수를 선언해주면 된다.

 

public class MyData implements Parcelable {

    public void writeToParcel( Parcel dest ) {

         // 여기서 저장할 내용을 순서대로 쓴다

         dest.writeString(xx);

 

    }

 

   public void readFromParcel( Parcel src ) {

         // 여기서 읽어올 내용을 순서대로 읽는다.

         xx = src.readString();

 

   }

 

    public static Pacelable.Creator<MyData> CREATOR = new Creator<MyData>() {

        public MyData createFromParcel( Parcel source ) {

            MyData data = new MyData();

            data.readFromParcel( source );

            return data;

         }

 

        public MyData[] newArray( int size ) {

            return new MyData[size];

        }

    }

}

 

 

전송하는 곳

Intent i = new Intent( this, Other.class );

i.putExtra( "name", data );

 

수신하는 곳

MyData data = getIntent().getParcelableExtra("name");

 

 

 

* Parcelable 객체를 변수로 가진 Parcelable

public class MyHome implements Parcelable {

    MyData data;

 

    public void writeToParcel( Parcel dest ) {

         dest.writeParcelable( data, 0 );

    }

 

   public void readFromParcel( Parcel src ) {

         data = src.readParcelable( MyData.class.getClassLoader() );

   }

 

    public static Pacelable.Creator<MyHome> CREATOR = new Creator<MyHome>() {

        public MyHome createFromParcel( Parcel source ) {

             MyHome data = new  MyHome();

            data.readFromParcel( source );

            return data;

         }

 

        public  MyHome[] newArray( int size ) {

            return new MyHome[size];

        }

    }

}

 

* ArrayList 를 읽고 쓰기

ArrayList<MyData> list;

 

public void writeToParcel( Parcel dest ) {

   dest.writeTypedList( list );

}

 

public void readFromParcel( Parcel src ) {

   src.readTypedList( list , MyData.CREATOR );

}

 

 

 

 

* HashMap 을 쓰고 읽기

 

HashMap<String, MyData> map = new HashMap<String, MyData>();

위에서 MyData는 parcelable로 구성되어 있다.

hashmap의 경우 key를 돌며, 데이터 내용을 번들에 추가해 전송하고,

키 목록은 문자열 배열로 만들어 전송한다.

 

public void writeToParcel( Parcel dest ) {

   if( map.size() > 0 ) {

      Set<String> keySet = map.keySet();

      Bundle b = new Bundle();

      for( String key : keySet ) {

          b.putParcelable( key, map.get(key) );

      }

 

     String[] array = keySet.toString( new String[keySet.size()]);

     dest.writeStringArray(array);

     dest.writeBundle(b);

   }

}

 

public void readFromParcel( Parcel src ) {

   String[] keys = src.createStringArray();

   Bundle b = src.readBundle( MyData.class.getClassLoader() );

   for( String key: keys ) {

      map.put( key, b.getParcelable( key ) );

   }

}

'프로그래밍 > Android' 카테고리의 다른 글

GCM 메시지 전송  (0) 2013.09.13
Dialog  (0) 2013.02.22
Fragment 기본  (0) 2012.12.28
EditText 특수문자 입력 제한  (0) 2012.09.10
앱 연동  (0) 2012.07.27
Data Animation???  (0) 2012.04.18
Assets 폴더의 이미지 읽기  (0) 2012.03.15
JSON  (0) 2012.01.31
HTTP POST/GET 몇가지 정리  (0) 2012.01.30
OpenGL es 조명  (0) 2011.12.07