Put Parcelable via intent - java

I am trying to pass Parcelable object from the A activity to B activity via intent:
Intent intent = new Intent (A.this, B.class);
intent.putExtra ("post", mypost); // where post implements Parcelable`
In B Activity I got the post object in this way:
Post myPost = getIntent().getParcelableExtra("post");
In B activity myPost object fields are mixed, e.g. I have postText and postDate fields in Post model, the values of this fields in B activity are mixed.
Why this can happen? My model class look likes the following:
public class Post implements Parcelable, Serializable {
private static final long serialVersionUID = 2L;
#SerializedName("author")
private User author;
#SerializedName("comments_count")
private String commentsCount;
#SerializedName("image")
private String imageToPost;
#SerializedName("parent_key")
private String parentKey;
#SerializedName("created_date")
private String postDate;
#SerializedName("id")
private String postId;
#SerializedName("text")
private String postText;
#SerializedName("title")
private String postTitle;
#SerializedName("shared_post_id")
private String sharedPostId;
#SerializedName("url")
private String urlToPost;
#SerializedName("video")
private String videoToPost;
public Post() {
}
public Post(Parcel in) {
author = (User) in.readValue(getClass().getClassLoader());
commentsCount = in.readString();
imageToPost = in.readString();
parentKey = in.readString();
postDate = in.readString();
postId = in.readString();
postText = in.readString();
postTitle = in.readString();
sharedPostId = in.readString();
urlToPost = in.readString();
videoToPost = in.readString();
}
public static final Creator<Post> CREATOR = new Creator<Post>() {
#Override
public Post createFromParcel(Parcel in) {
return new Post(in);
}
#Override
public Post[] newArray(int size) {
return new Post[size];
}
};
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public String getPostDate() {
return postDate;
}
public void setPostDate(String postDate) {
this.postDate = postDate;
}
public String getPostTitle() {
return postTitle;
}
public void setPostTitle(String postTitle) {
this.postTitle = postTitle;
}
public String getPostText() {
return postText;
}
public void setPostText(String postText) {
this.postText = postText;
}
public String getPostId() {
return postId;
}
public void setPostId(String postId) {
this.postId = postId;
}
public String getUrlToPost() {
return urlToPost;
}
public void setUrlToPost(String urlToPost) {
this.urlToPost = urlToPost;
}
public String getImageToPost() {
return imageToPost;
}
public void setImageToPost(String imageToPost) {
this.imageToPost = imageToPost;
}
public String getVideoToPost() {
return videoToPost;
}
public void setVideoToPost(String videoToPost) {
this.videoToPost = videoToPost;
}
public String getParentKey() {
return parentKey;
}
public void setParentKey(String parentKey) {
this.parentKey = parentKey;
}
public String getCommentsCount() {
return commentsCount;
}
public void setCommentsCount(String commentsCount) {
this.commentsCount = commentsCount;
}
public String getSharedPostId() {
return sharedPostId;
}
public void setSharedPostId(String sharedPostId) {
this.sharedPostId = sharedPostId;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(author);
dest.writeString(commentsCount);
dest.writeString(imageToPost);
dest.writeString(parentKey);
dest.writeString(postDate);
dest.writeString(postId);
dest.writeString(postText);
dest.writeString(postTitle);
dest.writeString(sharedPostId);
dest.writeString(urlToPost);
dest.writeString(videoToPost);
}
}

Add describeContents and writeToParcel.
Examples:
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(email);
dest.writeString(pass);
dest.writeFloat(amountPaid);
dest.writeString(url);
dest.writeInt(age);
}

Related

how to send arraylist to another activity through parcable

I have a model class which implement Parcelable and 2 string and one arraylist of another model class
i can retrive title1 and title2 but i am not able to retrive option4 arraylist its get null
`public class TopListSubListModel implements Parcelable {
String title1,title2;
ArrayList option4;
public TopListSubListModel(String title1, String title2, ArrayList<Option4Model> option4) {
this.title1 = title1;
this.title2 = title2;
this.option4 = option4;
}
protected TopListSubListModel(Parcel in) {
title1 = in.readString();
title2 = in.readString();
}
public static final Creator<TopListSubListModel> CREATOR = new Creator<TopListSubListModel>() {
#Override
public TopListSubListModel createFromParcel(Parcel in) {
return new TopListSubListModel(in);
}
#Override
public TopListSubListModel[] newArray(int size) {
return new TopListSubListModel[size];
}
};
public String getTitle1() {
return title1;
}
public void setTitle1(String title1) {
this.title1 = title1;
}
public String getTitle2() {
return title2;
}
public void setTitle2(String title2) {
this.title2 = title2;
}
public ArrayList<Option4Model> getOption4() {
return option4;
}
public void setOption4(ArrayList<Option4Model> option4) {
this.option4 = option4;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title1);
parcel.writeString(title2);
}
}`

How do I get class file variable to another activity and access via button using intent not putExtra?

my problem is when I scan Qr code, I want to get the class file variables
and send via btnSearch button to an another activity. plz help me how to do it? sorry for poor English
This is My class file
public class UploadDataGetter {
#SerializedName("record_id")
private int id;
#SerializedName("full_name")
private static String name;
#SerializedName("full_address")
private String address;
#SerializedName("contact")
private String contact;
#SerializedName("dilivery_place")
private String dplace;
#SerializedName("dilivery_place")
private String fdetails;
#SerializedName("no_of_normal")
private int noOfNormal;
#SerializedName("no_of_classified")
private int noOfClassified;
#SerializedName("no_of_cds")
private int noOfCds;
public UploadDataGetter(
//constuctor's para
int record_id,
String full_name,
String full_address,
String contact_number,
String dilivery_place ,
String further_details,
int no_of_normal,
int no_of_classified,
int no_of_cds
)
{
id = record_id;
name = full_name;
address = full_address;
contact = contact_number;
dplace = dilivery_place;
fdetails = further_details;
noOfNormal = no_of_normal;
noOfClassified= no_of_classified;
noOfCds = no_of_cds;
String img_collector ;
String dilivery_status ;
double geo_long;
double geo_lat;
}
public
int getId() {
return id;
}
public
void setId(int id) {
this.id = id;
}
public static
String getName() {
return name;
}
public
void setName(String name) {
this.name = name;
}
public
String getAddress() {
return address;
}
public
void setAddress(String address) {
this.address = address;
}
public
String getContact() {
return contact;
}
public
void setContact(String contact) {
this.contact = contact;
}
public
String getDplace() {
return dplace;
}
public
void setDplace(String dplace) {
this.dplace = dplace;
}
public
String getFdetails() {
return fdetails;
}
public
void setFdetails(String fdetails) {
this.fdetails = fdetails;
}
public
int getNoOfNormal() {
return noOfNormal;
}
public
void setNoOfNormal(int noOfNormal) {
this.noOfNormal = noOfNormal;
}
public
int getNoOfClassified() {
return noOfClassified;
}
public
void setNoOfClassified(int noOfClassified) {
this.noOfClassified = noOfClassified;
}
public
int getNoOfCds() {
return noOfCds;
}
public
void setNoOfCds(int noOfCds) {
this.noOfCds = noOfCds;
}
this is my Qr Activity,
public class QrActivity extends AppCompatActivity {
public static UploadDataGetter id;
public static UploadDataGetter name;
public static UploadDataGetter address;
public static UploadDataGetter dplace;
public static UploadDataGetter fdetails;
public static UploadDataGetter noOfNormal;
public static UploadDataGetter noOfClassified;
public static UploadDataGetter noOfCds;
public static UploadDataGetter img_collector;
public static UploadDataGetter dilivery_status;
/**
* QR code declaration
*/
Button btnscan, btnSearch;
TextView lblSearch;
TextView ID;
TextView DPlace;
TextView Address;
TextView name;
TextView contact;
private Object uploadDataGetter;
private String result;
#Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
btnscan = (Button) findViewById(R.id.btnscan);
btnscan.setClickable(false);
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setClickable(false);
lblSearch = (TextView) findViewById(R.id.lblSearch);
final Activity activity = this;
btnscan.setOnClickListener(new View.OnClickListener() {
#Override
public
void onClick(View v) {
btnscan.setClickable(true);
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
//call the class var
if (this.getIntent().getAction().equals("moveData"))
moveData();
}
private void moveData() {
String id = uploadDataGetter.toString();
String name = uploadDataGetter.toString();
String address = uploadDataGetter.toString();
String dplace = uploadDataGetter.toString();
String noOfNormal= uploadDataGetter.toString();
String noOfClassified = uploadDataGetter.toString();
String noOfCds = uploadDataGetter.toString();
//this is for Calling class file
}
#Override
protected
void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_SHORT).show();
} else {
/**
* Qr code result
* */
lblSearch.setText(result.getContents());
// Toast.makeText(this, result.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
// "Go to Second Activity" button click
View.OnClickListener listener = new View.OnClickListener() {
#Override
public
void onClick(View v) {
btnSearch.setClickable(true);
Intent myIntent= new Intent(QrActivity.this, DistributionDetails.class);
myIntent.setAction("moveData");
startActivity(myIntent);
}
};
btnscan.setOnClickListener(listener);
btnSearch.setOnClickListener(listener);
}
public
void getResults(View view) {
}
}
Replace your class with this
public class UploadDataGetter implements Serializable {
#SerializedName("record_id")
private int id;
#SerializedName("full_name")
private String name;
#SerializedName("full_address")
private String address;
#SerializedName("contact")
private String contact;
#SerializedName("dilivery_place")
private String dplace;
#SerializedName("dilivery_place")
private String fdetails;
#SerializedName("no_of_normal")
private int noOfNormal;
#SerializedName("no_of_classified")
private int noOfClassified;
#SerializedName("no_of_cds")
private int noOfCds;
public UploadDataGetter(
//constuctor's para
int record_id,
String full_name,
String full_address,
String contact_number,
String dilivery_place,
String further_details,
int no_of_normal,
int no_of_classified,
int no_of_cds
) {
id = record_id;
name = full_name;
address = full_address;
contact = contact_number;
dplace = dilivery_place;
fdetails = further_details;
noOfNormal = no_of_normal;
noOfClassified = no_of_classified;
noOfCds = no_of_cds;
String img_collector;
String dilivery_status;
double geo_long;
double geo_lat;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getDplace() {
return dplace;
}
public void setDplace(String dplace) {
this.dplace = dplace;
}
public String getFdetails() {
return fdetails;
}
public void setFdetails(String fdetails) {
this.fdetails = fdetails;
}
public int getNoOfNormal() {
return noOfNormal;
}
public void setNoOfNormal(int noOfNormal) {
this.noOfNormal = noOfNormal;
}
public int getNoOfClassified() {
return noOfClassified;
}
public void setNoOfClassified(int noOfClassified) {
this.noOfClassified = noOfClassified;
}
public int getNoOfCds() {
return noOfCds;
}
public void setNoOfCds(int noOfCds) {
this.noOfCds = noOfCds;
}
}
now send this to new activity in intent like this
Intent intent=new Intent(this, NewActivity.class);
int.putExtra("data", YOUR_UPDATE_DATA_GETTER_OBJECT);
Now get the object in the new activity like this
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class NewActivity extends Activity {
private UploadDataGetter dataGetter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
if(intent.hasExtra("data")){
dataGetter= (UploadDataGetter) intent.getSerializableExtra("data");
Log.d("NewActivity","ID: "+dataGetter.getId());
}
}
}

How to implement Parcelable

Good day. I need to implement parcelable in Model class.Currently it is Serializable. for now only setdate and set title if thare If any one can help. please edit code.
MainActivity.java
Document document = Jsoup.connect("http://feeds.bbci.co.uk/urdu/rss.xml").ignoreHttpErrors(true).get();
Elements itemElements = document.getElementsByTag("item");
for (int i = 0; i < itemElements.size(); i++) {
Element item = itemElements.get(i);
NewsItem newsItem = new NewsItem();
newsItem.setDate(item.child(4).text());
newsItem.setTitle(item.child(0).text());
newsItemsList.add(newsItem);
}
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter = new NewsAdaptor(Main2Activity.this,
newsItemsList);
lvRss.setAdapter(adapter);
}
});
return null;
}
NewsItem.java //model class
public class NewsItem implements Serializable {
String imagePath;
String title;
String link;
String date;
public NewsItem () {
}
public String getImagePath () {
return imagePath;
}
public void setImagePath ( String imagePath ) {
this.imagePath = imagePath;
}
public String getTitle () {
return title;
}
public void setTitle ( String title ) {
this.title = title;
}
public String getLink () {
return link;
}
public void setLink ( String link ) {
this.link = link;
}
public String getDate () {
return date;
}
public void setDate ( String date ) {
this.date = date;
}
NewsAdapter.java
public class NewsAdaptor extends BaseAdapter {
private int textSize;
TextView tvtitle;
private int color;
Context context;
public NewsAdaptor ( Context context, ArrayList <NewsItem> newsList ) {
this.context = context;
this.newsList = newsList;
this.color = Color.RED;
}
ArrayList<NewsItem> newsList;
#Override
public int getCount () {
return newsList.size();
}
#Override
public Object getItem ( int position ) {
return newsList.get(position);
}
#Override
public long getItemId ( int position ) {
return 0;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
if (convertView == null){
convertView=View.inflate(context, R.layout.newsitemlist_layout,null);
}
NewsItem currentNews = newsList.get(position);
ImageView iv1 = (ImageView) convertView.findViewById(R.id.mainimg);
TextView tvdate = (TextView) convertView.findViewById(R.id.pubDateid);
Picasso.with(context).load(currentNews.getImagePath()).placeholder(R.drawable.expressimg).into(iv1);
tvdate.setText(currentNews.getDate());
tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
tvtitle.setText(currentNews.getTitle());
tvtitle.setTextColor(color);
return convertView;
}
public void setTextColor(int color) {
this.color = color;
}
Check this Parcelable NewsItem:
public class NewsItem implements Parcelable {
String imagePath;
String title;
String link;
String date;
public NewsItem() {
}
protected NewsItem(Parcel in) {
imagePath = in.readString();
title = in.readString();
link = in.readString();
date = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(imagePath);
dest.writeString(title);
dest.writeString(link);
dest.writeString(date);
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<NewsItem> CREATOR = new Creator<NewsItem>() {
#Override
public NewsItem createFromParcel(Parcel in) {
return new NewsItem(in);
}
#Override
public NewsItem[] newArray(int size) {
return new NewsItem[size];
}
};
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}

Parcelable of Custom Classes

I am currently trying to pass an ArrayList of the class event into another activity as follows.events is an ArrayList of the Event class.
Intent i = new Intent(ViewEvents.this, UserFeed.class);
i.putParcelableArrayListExtra("events",events);
startActivity(i);
I am then retrieving the data as follows:
Intent i = getIntent();
ArrayList<Event> events = i.getParcelableArrayListExtra("events");
My Issue is that I get an Umarshing unknown type code 32 at offset 5788. I am not too sure how to fix this or why this is occurring. I have posted all the code for all three classes down below.Any help would be appreciated to solve this issue.
public class Event implements Parcelable {
public String tvEventName;
public String tvEventInfo;
public String tvDescription;
public String ivEventImage;
public String organizerName;
public String eventId;
public String veneuId;
public String organizerId;
public Venue venue;
public Organizer organizer;
public Event(String tvEventName, String tvEventInfo, String tvDescription, String ivEventImage, String eventId, String veneuId,Venue venue,Organizer organizer) {
this.tvEventName = tvEventName;
this.tvEventInfo = tvEventInfo;
this.tvDescription = tvDescription;
this.ivEventImage = ivEventImage;
this.eventId = eventId;
this.veneuId = veneuId;
}
public Event() {
}
protected Event(Parcel in) {
tvEventName = in.readString();
tvEventInfo = in.readString();
tvDescription = in.readString();
ivEventImage = in.readString();
organizerName = in.readString();
eventId = in.readString();
veneuId = in.readString();
organizerId = in.readString();
}
public static final Creator<Event> CREATOR = new Creator<Event>() {
#Override
public Event createFromParcel(Parcel in) {
return new Event(in);
}
#Override
public Event[] newArray(int size) {
return new Event[size];
}
};
public static Event fromJSON(JSONObject jsonObject)throws JSONException {
Event event = new Event();
//Getting the name of the event
JSONObject nameEvent = jsonObject.getJSONObject("name");
event.tvEventName = nameEvent.getString("text");
//Getting the description for the event Time and location only
JSONObject eventInfo = jsonObject.getJSONObject("start");
event.tvEventInfo = eventInfo.getString("utc");
SimpleDateFormat existingUTCFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat requiredFormat = new SimpleDateFormat("MM-dd hh:mm a");
event.eventId = jsonObject.getString("id");
try{
Date getDate = existingUTCFormat.parse(event.tvEventInfo);
String mydate = requiredFormat.format(getDate);
event.tvEventInfo = mydate;
}
catch(ParseException e){
e.printStackTrace();
}
//Getting the description of the event
JSONObject eventDescription = jsonObject.getJSONObject("description");
event.tvDescription = eventDescription.getString("text");
//Getting a thumbnail of the image for futer use.
try{
jsonObject.getJSONObject("logo");
JSONObject logo = jsonObject.getJSONObject("logo");
JSONObject original= logo.getJSONObject("original");
event.ivEventImage = original.getString("url");
Log.i("Ingo",event.ivEventImage);
}
catch (Exception exception){
event.ivEventImage ="#drawable/tree";
}
event.veneuId = jsonObject.getString("venue_id");
event.organizerId = jsonObject.getString("organizer_id");
return event;
}
public Organizer getOrganizer() {
return organizer;
}
public void setOrganizer(Organizer organizer) {
this.organizer = organizer;
}
public String getOrganizerId() {
return organizerId;
}
public void setOrganizerId(String organizerId) {
this.organizerId = organizerId;
}
public String getTvEventName() {
return tvEventName;
}
public void setTvEventName(String tvEventName) {
this.tvEventName = tvEventName;
}
public String getTvEventInfo() {
return tvEventInfo;
}
public void setTvEventInfo(String tvEventInfo) {
this.tvEventInfo = tvEventInfo;
}
public Venue getVenue() {
return venue;
}
public void setVenue(Venue venue) {
this.venue = venue;
}
public String getTvDescription() {
return tvDescription;
}
public void setTvDescription(String tvDescription) {
this.tvDescription = tvDescription;
}
public String getIvEventImage() {
return ivEventImage;
}
public void setIvEventImage(String ivEventImage) {
this.ivEventImage = ivEventImage;
}
public String getVeneuId() {
return veneuId;
}
public void setVeneuId(String veneuId) {
this.veneuId = veneuId;
}
public String getOrganizerName() {
return organizerName;
}
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public void setOrganizerName(String organizerName) {
this.organizerName = organizerName;
}
#Override
public int describeContents() {
return 0;
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(tvEventName);
dest.writeString(tvEventInfo);
dest.writeString(tvDescription);
dest.writeString(ivEventImage);
dest.writeString(organizerName);
dest.writeString(eventId);
dest.writeString(veneuId);
dest.writeString(organizerId);
dest.writeParcelable( this.venue,flags);
dest.writeParcelable(this.organizer,flags);
}
}
CLASS 2
public class Venue implements Parcelable {
public String address;
public String city;
public String region;
public String postalCode;
public String country;
public String latitude;
public String longitude;
public String simpleAddress;
public Venue() {
}
public Venue(String address, String city, String region, String postalCode, String country, String latitude, String longitude, String simpleAddress) {
this.address = address;
this.city = city;
this.region = region;
this.postalCode = postalCode;
this.country = country;
this.latitude = latitude;
this.longitude = longitude;
this.simpleAddress = simpleAddress;
}
protected Venue(Parcel in) {
address = in.readString();
city = in.readString();
region = in.readString();
postalCode = in.readString();
country = in.readString();
latitude = in.readString();
longitude = in.readString();
simpleAddress = in.readString();
}
public static final Creator<Venue> CREATOR = new Creator<Venue>() {
#Override
public Venue createFromParcel(Parcel in) {
return new Venue(in);
}
#Override
public Venue[] newArray(int size) {
return new Venue[size];
}
};
public static Venue fromJSON(JSONObject jsonObject)throws JSONException {
Venue venue = new Venue();
if(jsonObject.getString("address_1") == null){
if(jsonObject.getString("address_2") == null)
venue.address = "No Location Available";
else
venue.address = jsonObject.getString("address_2");
}
else
venue.address = jsonObject.getString("address_1");
venue.city = jsonObject.getString("city");
venue.region = jsonObject.getString("region");
venue.postalCode = jsonObject.getString("postal_code");
venue.country = jsonObject.getString("country");
venue.latitude = jsonObject.getString("latitude");
venue.longitude = jsonObject.getString("longitude");
venue.simpleAddress =venue.address +","+ venue.city +","+ venue.country;
Log.i("SIMPLE", venue.simpleAddress);
return venue;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getSimpleAddress() {
return simpleAddress;
}
public void setSimpleAddress(String simpleAddress) {
this.simpleAddress = simpleAddress;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(address);
dest.writeString(city);
dest.writeString(region);
dest.writeString(postalCode);
dest.writeString(country);
dest.writeString(latitude);
dest.writeString(longitude);
dest.writeString(simpleAddress);
}
}
CLASS 3
public class Organizer implements Parcelable{
public String description;
public String organizerId;
public String numePastEvents;
public String numFutureEvents;
public String website;
public String facebookUsername;
public String twitter;
public String name;
public Organizer() {
}
protected Organizer(Parcel in) {
description = in.readString();
organizerId = in.readString();
numePastEvents = in.readString();
numFutureEvents = in.readString();
website = in.readString();
facebookUsername = in.readString();
twitter = in.readString();
name = in.readString();
}
public static final Creator<Organizer> CREATOR = new Creator<Organizer>() {
#Override
public Organizer createFromParcel(Parcel in) {
return new Organizer(in);
}
#Override
public Organizer[] newArray(int size) {
return new Organizer[size];
}
};
public static Organizer fromJson(JSONObject jsonObject){
Organizer organizer = new Organizer();
try {
organizer.description = jsonObject.getJSONObject("description").getString("text");
} catch (JSONException e) {
organizer.description ="NA";
e.printStackTrace();
}
try {
organizer.organizerId = jsonObject.getString("id");
} catch (JSONException e) {
organizer.organizerId = "NA";
e.printStackTrace();
}
try {
organizer.numePastEvents = jsonObject.getString("num_past_events");
} catch (JSONException e) {
organizer.numePastEvents = "Na";
e.printStackTrace();
}
try {
organizer.numFutureEvents = jsonObject.getString("num_future_events");
} catch (JSONException e) {
organizer.numFutureEvents = "NA";
e.printStackTrace();
}
try {
organizer.website = jsonObject.getString("website");
} catch (JSONException e) {
Log.i("Error",e.getMessage());
organizer.website = "NA";
e.printStackTrace();
}
try {
organizer.facebookUsername = jsonObject.getString("facebook");
} catch (JSONException e) {
organizer.facebookUsername = "NA";
e.printStackTrace();
}
try {
organizer.name = jsonObject.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
try {
organizer.twitter = jsonObject.getString("twitter");
} catch (JSONException e) {
e.printStackTrace();
}
return organizer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTwitter() {
return twitter;
}
public void setTwitter(String twitter) {
this.twitter = twitter;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getOrganizerId() {
return organizerId;
}
public void setOrganizerId(String organizerId) {
this.organizerId = organizerId;
}
public String getNumePastEvents() {
return numePastEvents;
}
public void setNumePastEvents(String numePastEvents) {
this.numePastEvents = numePastEvents;
}
public String getNumFutureEvents() {
return numFutureEvents;
}
public void setNumFutureEvents(String numFutureEvents) {
this.numFutureEvents = numFutureEvents;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getFacebookUsername() {
return facebookUsername;
}
public void setFacebookUsername(String facebookUsername) {
this.facebookUsername = facebookUsername;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(description);
dest.writeString(organizerId);
dest.writeString(numePastEvents);
dest.writeString(numFutureEvents);
dest.writeString(website);
dest.writeString(facebookUsername);
dest.writeString(twitter);
dest.writeString(name);
}
When implementing the Parcelable interface, it is an absolute requirement that your reads and writes exactly match each other. In your case, this is your Event(Parcel) constructor and void writeToParcel().
However, your writeToParcel() implementation includes two writes that your constructor does not read.
dest.writeParcelable( this.venue,flags);
dest.writeParcelable(this.organizer,flags);
You have to either remove these or add matching reads to your constructor.
venue = in.readParcelable(Venue.class.getClassLoader());
organizer = in.readParcelable(Organizer.class.getClassLoader());

Unable to get Parcelable list in a bundle in another activity

What I want is to pass a list of list from one activity to another. Here is my approach.
Feeditem.java
public class FeedItem implements Parcelable {
private String id,status, image, timeStamp, url;
private ArrayList<CommentItem> commentItems;
public FeedItem() {
}
protected FeedItem(Parcel in) {
id = in.readString();
status = in.readString();
image = in.readString();
timeStamp = in.readString();
url = in.readString();
if(commentItems!=null) {
in.createTypedArrayList(CommentItem.CREATOR);
}
}
public static final Creator<FeedItem> CREATOR = new Creator<FeedItem>() {
#Override
public FeedItem createFromParcel(Parcel in) {
return new FeedItem(in);
}
#Override
public FeedItem[] newArray(int size) {
return new FeedItem[size];
}
};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImge() {
return image;
}
public void setImge(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public ArrayList<CommentItem> getCommentItems() {
return commentItems;
}
public void setCommentItems(ArrayList<CommentItem> commentItems)
{
this.commentItems=commentItems;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(status);
dest.writeString(image);
dest.writeString(timeStamp);
dest.writeString(url);
dest.writeTypedList(commentItems);
}}
CommentItem.java
public class CommentItem implements Parcelable{
private String id,comment, from, timeStamp;
public CommentItem() {
}
protected CommentItem(Parcel in) {
id = in.readString();
comment = in.readString();
from = in.readString();
timeStamp = in.readString();
}
public static final Creator<CommentItem> CREATOR = new Creator<CommentItem>() {
#Override
public CommentItem createFromParcel(Parcel in) {
return new CommentItem(in);
}
#Override
public CommentItem[] newArray(int size) {
return new CommentItem[size];
}
};
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(comment);
dest.writeString(from);
dest.writeString(timeStamp);
}}
ReceivingActivity.java
ArrayList<CommentItem> commentItems;
FeedItem feedItem;
commentItems=new ArrayList<>();
Bundle bundle=getIntent().getExtras();
feedItem=bundle.getParcelable("status");
if(feedItem.getCommentItems()!=null) { //help here
commentItems = feedItem.getCommentItems();
}
SendingActivity.java
Button getComments=(Button)convertView.findViewById(R.id.commentsbutton);
getComments.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(activity, CommentActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("status", item); //item is feeditem
i.putExtras(bundle);
activity.startActivity(i);
}
});
I am able to get all the attributes of the feeditem except the list which is giving me null in the receiving activity. Could someone please help me out with this. Thanks in advance.
I think the problem is that you're only reading in the list if it is already non-null:
if(commentItems!=null) {
in.createTypedArrayList(CommentItem.CREATOR);
}
If it's null, which it will be at that point, it doesn't get set. Just remove the non-null check and I think it will work.
commentItems = in.createTypedArrayList(CommentItem.CREATOR);

Categories