Android Parcelable -- Retailerorderactivity.Java Return Null

Android Parcelable -- RetailerOrderActivity.java return null

Here is how you can pass an ArrayList as I tried it.

MyListClass.java - Custom class

public class MyListClass implements Parcelable{

private int test;

public MyListClass()
{}

public MyListClass(Parcel read){
test = read.readInt();
}

public int getTest() {
return test;
}

public void setTest(int test) {
this.test = test;
}

public static final Parcelable.Creator<MyListClass> CREATOR =
new Parcelable.Creator<MyListClass>() {

@Override
public MyListClass createFromParcel(Parcel source) {
return new MyListClass(source);
}

@Override
public MyListClass[] newArray(int size) {
return new MyListClass[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel arg0, int arg1) {
arg0.writeInt(test);
}
}

MyParcelable.java

public class MyParcelable implements Parcelable {

private List<MyListClass> arrList = new ArrayList<MyListClass>();
private int myInt = 0;
private String str = null;

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

public List<MyListClass> getArrList() {
return arrList;
}

public void setArrList(List<MyListClass> arrList) {
this.arrList = arrList;
}

public int getMyInt() {
return myInt;
}

public void setMyInt(int myInt) {
this.myInt = myInt;
}

MyParcelable() {
// initialization
arrList = new ArrayList<MyListClass>();
}

public MyParcelable(Parcel in) {
myInt = in.readInt();
str = in.readString();
in.readTypedList(arrList, MyListClass.CREATOR);
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel outParcel, int flags) {
outParcel.writeInt(myInt);
outParcel.writeString(str);
outParcel.writeTypedList(arrList);
}

public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {

@Override
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}

@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
}

MainAcitivty.java

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

arrList.add(new MyListClass());
arrList.get(0).setTest(200);

MyParcelable object = new MyParcelable();
object.setMyInt(100);
object.setArrList(arrList);

Intent intent = new Intent(MainActivity.this,ReceiverParcel.class);
intent.putExtra("parcel", object);
startActivity(intent);
}

ReceiverParcel.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle b = getIntent().getExtras();
MyParcelable object = b.getParcelable("parcel");

System.out.println(object.getArrList().get(0).getTest());
System.out.println(object.getMyInt());
}

Android Parcelable object return Null

I guess you misunderstood the Parcelable examples.
You have to put all needed elements into the parcel, and get it from it later:

To write your object to the parcel, this is needed:

public void writeToParcel(Parcel dest, int flags) 
{
dest.writeString(productCodce);
dest.writeString(description);
// and all other elements
}

Plus, you need a constructor receiving a parcel:

public Product(Parcel in)
{
this.productCode=in.readString();
this.description=in.readString();
// and all other elements
}

Your Creator should be something like:

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() 
{
public Product createFromParcel(Parcel in) { return new Product(in); }
public Product[] newArray(int size) { return new Product[size]; }
};

Now, at your activity level (NOT in your Product class!):

Push it into extras, use:

bundle.putParcelableArrayList("whatevername",products);

To get it from the extras, use a simple:

ArrayList<Product> products=bundle.getParcelableArrayList("whatevername");

Implement Parcelable for a List Map String, String

You could extend Map and implement Parcelable for it. When writing to parcel, you could write elements count as a first int, then iterate over entries and add them one after another like:

destParcelable.writeInt(size());
for (final Entry<String, String> entry : getEntries()) {
destParcelable.writeString(entry.getKey());
destParcelable.writeString(entry.getValue());
}

When reading from parcel just read the first int (that will be your entries count) and start a loop reading key and values one by one:

final int size = srcParcelable.readInt();
for (int i=0; i<size; i++) {
put(srcParcelable.readString(), srcParcelable.readString());
}

And I believe there is a method to add list of parcelables to bundle, and since your map is a parcelable now it won't be any problems.

ClassCastException when trying to pass custom object implementing parcelable to another activity using Intent

You cannot cast an object array (which is what waveList.toArray() gives you) directly to an array of another type. You only need to cast to SineWave too because it already is a Parcelable. I believe this is how you can do it:

i1.putExtra("list", waveList.toArray(new SineWave[waveList.size()]));

For info, the documentation for putExtra states:

Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

I don't know what impact that might have, but you may need to add your package name in front of "list" as well.

Passing ArrayList CustomObject Between Activities

This code

Bundle informacion= new Bundle();
informacion.putParcelableArrayList("eventos", ArrayList<Eventos> eventos);
intent.putExtras(informacion);

Should be

Bundle informacion = new Bundle();
ArrayList<Eventos> mas = new ArrayList<Eventos>();
informacion.putSerializable("eventos", mas);
intent.putExtras(informacion);

and Make sure your Eventos structure like a serializable object

private class Eventos implements Serializable {

}

Reading Values

ArrayList<Eventos> madd=getIntent().getSerializableExtra(key);


Related Topics



Leave a reply



Submit