我刚开发了我的第一个实现接口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