我剛開發了我的第一個實現接口Parcelable的Parcelable類。 我真的很開心,因爲它能正常工作。 也許我的解決方案可以幫助任何人:
public class DealCategory implements Parcelable {
private int categoryID;
private String categoryName;
private List<DealCategory> listaCategoriasSeleccionadas = new ArrayList<DealCategory>();
/**
* GET/SET
*/
//-----------------------------------------------------------|
//-----------------------------------------------------------|
//------------------- METHODS FOR PARCELABLE ----------------|
//-----------------------------------------------------------|
//-----------------------------------------------------------|
/*
* (non-Javadoc)
* @see android.os.Parcelable#describeContents()
* Implementacion de los metodos de la Interfaz Parcelable
*/
@Override
public int describeContents() {
return hashCode();
}
/*
* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
* IMPORTANT
* We have to use the same order both TO WRITE and TO READ
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(categoryID);
dest.writeString(categoryName);
dest.writeTypedList(listaCategoriasSeleccionadas);
}
/*
* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
* IMPORTANT
* We have to use the same order both TO WRITE and TO READ
*
* We reconstruct the object reading from the Parcel data
*/
public DealCategory(Parcel p) {
categoryID = p.readInt();
categoryName = p.readString();
p.readTypedList(listaCategoriasSeleccionadas, DealCategory.CREATOR);
}
/*
* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
* We need to add a Creator
*/
public static final Parcelable.Creator<DealCategory> CREATOR = new Parcelable.Creator<DealCategory>() {
@Override
public DealCategory createFromParcel(Parcel parcel) {
return new DealCategory(parcel);
}
@Override
public DealCategory[] newArray(int size) {
return new DealCategory[size];
}
};
}
我送(寫)對象Parcelable 「DealCategory」 從活動A到活動B
protected void returnParams(DealCategory dc) {
Intent intent = new Intent();
intent.putExtra("Category", dc);
setResult(REQUEST_CODE_LISTA_DEALS, intent);
finish()
}
我接受(讀取)對象Parcelable 「DealCategory」來自活動A的活動B
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle b = data.getExtras();
DealCategory dc = (DealCategory) b.getParcelable("Category");
我檢查是否收到更正的值。我temporaly顯示他們在日誌
for (int i = 0; i < dc.getListaCategorias().size(); i++) {
Log.d("Selected Category", "ID: " + dc.getListaCategorias().get(i).getCategoryID() + " -- NAME:" + dc.getListaCategorias().get(i).getCategoryName());
lR += dc.getListaCategorias().get(i).getCategoryName() +", ";
}
} //Close onActivityResult
您能打印異常嗎?在logcat中粘貼logcat :) – 2013-05-10 03:25:09
當訪問UD的方法時它是一個'nullPointException',但問題應該立即解決!謝謝! – 2013-05-10 03:47:19
如果您的問題已解決,請將解決方案作爲答案添加並接受。 – Mudassir 2013-05-10 05:03:36