I need to send a list of objects from an activity to other activity with an Intent. I've implemented my class by Parcelable:
public class ListMainActivityHolder implements Parcelable {
String title ;
String date ;
String url ;
public ListMainActivityHolder(){}
public void setTitle(String t){
this.title = t ;
}
public ListMainActivityHolder(Parcel source){
title = source.readString();
setDate(source.readString());
url = source.readString();
}
public void setDate(String d){
if(d.length()>40){
d = d.substring(0, 30);
d = d + "..." ;
}
this.date = extractDate(d) ;
}
public void setUrl(String u){
this.url = u ;
}
public String getUrl(String u){
return this.url ;
}
public String getTitle(){
return this.title;
}
public String getDate(){
return this.date ;
}
public String extractDate(String date){
String day , month , year;
if(date.length()>10){
day = date.substring(5,7);
month = date.substring(8,11);
year = date.substring(12, 16);
if(month.equalsIgnoreCase("Jan")){
month = "01" ;
}else if(month.equalsIgnoreCase("Feb")){
month = "02" ;
}else if(month.equalsIgnoreCase("Mar")){
month = "03" ;
}else if(month.equalsIgnoreCase("Apr")){
month = "04" ;
}else if(month.equalsIgnoreCase("May")){
month = "05" ;
}else if(month.equalsIgnoreCase("Jun")){
month = "06" ;
}else if(month.equalsIgnoreCase("Jul")){
month = "07" ;
}else if(month.equalsIgnoreCase("Aug")){
month = "08" ;
}else if(month.equalsIgnoreCase("Sep")){
month = "09" ;
}else if(month.equalsIgnoreCase("Oct")){
month = "10" ;
}else if(month.equalsIgnoreCase("Nov")){
month = "11" ;
}else if(month.equalsIgnoreCase("Dec")){
month = "12" ;
}
}else{
day = "20" ;
month = "02" ;
year = "2014" ;
}
CalendarTool calender = new CalendarTool(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
String newdate = calender.getIranianDate();
return newdate ;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(title);
dest.writeString(date);
dest.writeString(url);
}
public static final Parcelable.Creator<ListMainActivityHolder> CREATOR
= new Parcelable.Creator<ListMainActivityHolder>() {
@Override
public ListMainActivityHolder createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return null;
}
@Override
public ListMainActivityHolder[] newArray(int size) {
// TODO Auto-generated method stub
return new ListMainActivityHolder[size];
}
};
}
at my activity:
Intent intent = getIntent();
listholder = intent.getParcelableExtra("MYLIST");
String str = listholder.get(1).getDate();
Log.e("Tag", str);
when I running my app I get these errors :
03-06 08:47:47.876: E/AndroidRuntime(3401): FATAL EXCEPTION: main 03-06 08:47:47.876: E/AndroidRuntime(3401): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@433bd068: Unmarshalling unknown type code 39 at offset 132