Moudiz Moudiz - 2 months ago 7
Android Question

android passing arguments are correct in first activity , wrong in the second

I am passing arguments from first activity to the second , in the first activity i can see all the arguments , but in the second activity one of the argument is being always

0
, the rest I can see them. I passing
id
,
url
,
likes
the likes argument is always null can you help indentify the issue ?

I am passing data like this , in systemoutprint I am getting value
2


Listitem listitem = new Listitem(item.getOrder(), item.getId(), item.getUrl(), item.getUserName(), item.getLikes());

Intent intent = new Intent(mcontext,SingleObjectActivity.class);

System.out.println(item.getUrl()); //i can see correct value
System.out.println(item.getLikes());//i can see correct value

intent.putExtra("Object_list", (Parcelable) listitem);


intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);


here is second activity the url is correct , the
likes
getting always
0


Listitem li = ((Listitem) getIntent().getParcelableExtra("Object_list"));
calc= li.getLikes();
id=li.getId();
System.out.println(li.getLikes());


here is the logcat

09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/System.out: http://justedhak.com/old-files/images/uploaded_images57d985894b416.png
09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/System.out: 2
09-14 20:21:00.069 8874-8874/com.justedhak.www.i I/Timeline: Timeline: Activity_launch_request id:com.justedhak.www.i time:16396709
09-14 20:21:00.199 8874-8874/com.justedhak.www.i I/System.out: 0


here is the listitem , I though might problem with the
writeToParcel


public Listitem(int order, String id, String url, String userName, int likes) {
this.id = id;
this.url = url;
this.userName = userName;
this.order = order;
this.likes = likes;
}

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

public void setUrl(String url) {
this.url = url;
}

public String getUrl() {
return url;
}

public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}


public int getLikes() {
return likes;
}

public void setLikes(int likes) {
this.likes = likes;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
// dest.writeString(this.name);
dest.writeString(this.url);
dest.writeInt(this.likes);

}

public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() {
public Listitem createFromParcel(Parcel in) {
return new Listitem(in);
}

public Listitem[] newArray(int size) {
return new Listitem[size];
}
};

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getOrder() {
return order;
}

public void setOrder(int order) {
this.order = order;
}


}

Answer

Your intent code seems to be correct.

You didn't show your read from parcel code, so it might be there too.

Regardless, parcelable code is a pain in the ass to do, so I suggest you to use a library that makes it automatically. This one github.com/johncarl81/parceler all you have to do is put the @Parcel annotation and use the Parcels.wrap and Parcels.unwrap static methods

example:

intent.putExtra("Object_list", Parcels.wrap(listitem));

// then

Listitem li = (Listitem) Parcels.unwrap(getIntent().getParcelableExtra("Object_list"));