- Today
- Total
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Link
- 재능이의 돈버는 일기
- StresslessLife
- K_JIN2SM
- 소소한 일상
- My Life Style & Memory a Box
- Blog's generation
- 공감 스토리
- 취객의 프로그래밍 연구실
- Love Me
- Dream Archive
- 세상에 발자취를 남기다 by kongmingu
- hanglesoul
- 카마의 IT 초행길
- 느리게.
- 미친듯이 즐겨보자..
- Joo studio
- Gonna be insane
- 악 다 날아갔어!! 갇대밋! 왓더...
- xopowo05
- 맑은공기희망운동
- 엔지니어 독립운동
- 혁준 블로그
- Simple in Complex with Simple
- 무의식이 의식을 지배한다
드럼치는 프로그래머
[안드로이드] Android Parcelable Example 본문
Few days back I had a requirement to send ArrayList of my Custom Class/Objects which need to be sent through Intent, I guess most of you also find this requirement now and then. I never thought it can be that tricky.
There are built in functions in Intent to send ArrayList of primitive objects e.g. String, Integer, but when it comes to Custom Data Handling Objects, BOOM… you need to take that extra pain…..
Android has defined a new light weight IPC (Inter Process Communication) data structure called Parcel, where you can flatten your objects in byte stream, same as J2SDK’s Serialization concept.
So let’s come back to my original requirement, I had a Data Handling class, which groups together a set of information-
public class ParcelData {
int id;
String name;
String desc;
String[] cities = {"suwon", "delhi"};
}
I want an ArrayList<ParcelData> of Data Handling objects to be able to pass through Intent. To do that, I can’t use the ParcelData as it is; you need to implement an Interface android.os.Parcelable which will make Objects of this class Parcelable. So you need to define your data handling class as-
public class ParcelData implements Parcelable {
int id;
String name;
String desc;
String[] cities = {"suwon", "delhi"};
}
You need to overwrite2 methods of android.os.Parcelable Interface-
describeContents()- define the kind of object you are going to Parcel, you can use the hashCode() here.
writeToParcel(Parcel dest, int flags)- actual object serialization/flattening happens here. You need to individually Parcel each element of the object.
public void writeToParcel(Parcel dest, int flags) {
Log.v(TAG, "writeToParcel..."+ flags);
dest.writeInt(id);
dest.writeString(name);
dest.writeString(desc);
dest.writeStringArray(cities);
}
Note: Don’t try things like, dest.writeValue(this) to flatten the complete Object at a time (I don’t think all people have some weird imaginations like me, so you’ll never try this, right ?…) that will end up in recursive call of writeToParcel().
Up to this point you are done with the required steps to flatten/serialize your custom object.
Next, you need to add steps to un-marshal/un-flatten/de-serialize (whatever you call it) your custom data objects from Parcel. You need to define one weird variable called CREATOR of type Parcelable.Creator. If you don’t do that, Android framework will through exception-
Parcelable protocol requires a Parcelable.Creator object called CREATOR
Following is a sample implementation of Parcelable.Creator<ParcelData> interface for my class ParcelData.java-
/**
* It will be required during un-marshaling data stored in a Parcel
* @author prasanta
*/
public class MyCreator implements Parcelable.Creator<ParcelData> {
public ParcelData createFromParcel(Parcel source) {
return new ParcelData(source);
}
public ParcelData[] newArray(int size) {
return new ParcelData[size];
}
}
You need to define a Constructor in ParcelData.java which puts together all parceled data back-
/**
* This will be used only by the MyCreator
* @param source
*/
public ParcelData(Parcel source){
/*
* Reconstruct from the Parcel
*/
Log.v(TAG, "ParcelData(Parcel source): time to put back parcel data");
id = source.readInt();
name = source.readString();
desc = source.readString();
source.readStringArray(cities);
}
You can deserve some rest now…...as you are done with it. So now you can use, something like-
Intent parcelIntent = new Intent(this, ParcelActivity.class);
ArrayList<ParcelData> dataList = new ArrayList<ParcelData>();
/*
* Add elements in dataLists e.g.
* ParcelData pd1 = new ParcelData();
* ParcelData pd2 = new ParcelData();
*
* fill in data in pd1 and pd2
*
* dataLists.add(pd1);
* dataLists.add(pd2);
*/
parcelIntent.putParcelableArrayListExtra("custom_data_list", data);
[출처] http://prasanta-paul.blogspot.kr/2010/06/android-parcelable-example.html
'★─Programing > ☆─Android' 카테고리의 다른 글
[안드로이드] Toast 기존 메시지 삭제, 현재 메시지만 보이도록 하는 방법 (0) | 2013.06.14 |
---|---|
[안드로이드] 리스트뷰 아이콘 클릭하여 순서 바꾸기 소스 (0) | 2013.06.04 |
[안드로이드] 액티비티(Activity) 투명 처리 하기 (0) | 2013.06.03 |
[안드로이드] WiFi Direct in Android 4.0 API Overview (번역) (0) | 2013.06.03 |
[안드로이드] 안드로이드 단말기 OS 버전 알아오기 (0) | 2013.06.03 |
Comments