액티비티간 데이터를 넘길 때 intent.putExtra, getExtra를 이용해서 넘기는데
난 문자열이나 정수, 실수형 데이터만 넘겨지는 줄 알았다.
문득 객체도 전달 가능한지 궁금하여 검색하니
역시나 객체도 전달 할 수 있었다.
Parcelable 인터페이스를 구현하는 객체를 만들어 전달하면 된다고 해서 따라해봤다.
////////////////////////////////////////////////////////////
먼저 Parcelable 인터페이스를 상속받아 넘길 객체의 클래스를 만든다
import android.os.Parcel;
import android.os.Parcelable;
public class SaleItem implements Parcelable{
private String RNUM;
private int PLNM_NO;
public SaleItem() {
}
private SaleItem(Parcel in) {
this.RNUM = in.readString();
this.PLNM_NO = in.readString();
}
public static final Parcelable.Creator<SaleItem> CREATOR = new Parcelable.Creator<SaleItem>() {
public SaleItem createFromParcel(Parcel in) {
return new SaleItem(in);
}
public SaleItem[] newArray (int size) {
return new SaleItem[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//생성자에서 읽어오는 순서와 기록하는 순서가 같아야 한다고 한다!!!!!
dest.writeString(this.RNUM);
dest.writeInt(this.PLNM_NO);
}
}
//////////////////////////////////////////////////////////
객체 넘길 때
Intent it = new Intent(context, SaleDetailActivity.class);
it.putExtra("sale_item", item);
context.startActivity(it);
//////////////////////////////////////////////////////////
객체 받을 때
SaleItem saleItem = (SaleItem) getIntent().getParcelableExtra("sale_item");
'공부 > Android' 카테고리의 다른 글
(android) webview clear cache (0) | 2017.12.24 |
---|---|
(android) RecyclerView bottom endless refresh[리스트 페이징) (2) | 2016.11.27 |
(android) recyclerview scroll이 느린 현상 (0) | 2016.11.08 |
(android) 키보드 내리기 (0) | 2016.11.07 |
(android) 키보드 올라올 때 버튼도 같이 올라오는 현상 (0) | 2016.11.07 |