1

I've been running into difficulties while trying to implement Parcelable to a number of my objects. So far I have a Project class with all of the String and integer variables and another object called ParcelableProject which contains a single Project class. My difficulty starts when I tried creating another Parcelable class by the name of AllProjects, this class contains a single array list which is made up of ParcelableProject objects.

I have this method by which I move Projects through intent:

    public void addProject(ParcelableProject p){
         Intent i = new Intent(this.getActivity(), ProjectsFragment.class);
         ArrayList<ParcelableProject> data = new ArrayList<ParcelableProject>();
         data.add(p);
         i.putParcelableArrayListExtra("projects", data);

    }

Furthermore, in my ProjectsFragment I try to access this ArrayList data via:

    Intent i = getActivity().getIntent();
    Bundle data=i.getExtras();
    ArrayList Projects=  data.getParcelable("projects");
    ParcelableProject pProj=Projects.get(0);

But it gives me a "cannot convert from Object to ParcelableProject" error. What is the proper way to access my Project's variables? There is going to be a list of them passed into my ProjectsFragment so the Parcelable needs to contain an ArrayList of Projects, I just don't know how to access the individual ones.

yanki
  • 375
  • 1
  • 3
  • 11
  • It also throws me a NullPointerException at ArrayList Projects= data.getParcelable("projects"); – yanki Feb 06 '14 at 07:15

2 Answers2

0

Do this way

Intent i = getIntent();
    Bundle data=i.getExtras();
    ArrayList<ParcelableProject> Projects= (ArrayList<ParcelableProject>) data.getParcelable("projects");

UPDATE try this

ArrayList<ParcelableProject> Projects= (ArrayList<ParcelableProject>) data.getParcelableArrayList("projects");
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • I did, but I still get the NullPointerException at Projects = (ArrayList) data.getParcelable("projects"); – yanki Feb 06 '14 at 07:32
0

Try this

public class EmployeeData implements Parcelable {

String empName;
String empId;
String empAddress;

public EmployeeData(Parcel in) {
    readFromParcel(in);
}

public EmployeeData(String name, String id, String address) {

    this.empName = name;
    this.empId = id;
    this.empAddress = address;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

private void readFromParcel(Parcel in) {

    empName = in.readString();
    empId = in.readString();
    empAddress = in.readString();

}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeString(empName);
    dest.writeString(empId);
    dest.writeString(empAddress);

}

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

    @Override
    public EmployeeData[] newArray(int size) {
        // TODO Auto-generated method stub
        return new EmployeeData[size];
    }

    @Override
    public EmployeeData createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return new EmployeeData(source);
    }
};

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public String getEmpId() {
    return empId;
}

public void setEmpId(String empId) {
    this.empId = empId;
}

public String getEmpAddress() {
    return empAddress;
}

public void setEmpAddress(String empAddress) {
    this.empAddress = empAddress;
}

}

Put object like:

EmployeeData empData = new EmployeeData(name, id, address);

            Bundle bundle = new Bundle();
            bundle.putParcelable("DATA", empData);

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtras(bundle);

get object like:

EmployeeData empData = getIntent().getParcelableExtra("DATA");
Sonali8890
  • 2,005
  • 12
  • 16
  • I am working with Fragments, when I make new Intent my two variables are this.getActivity() and Fragment.class. I am not sure if the second variable will work, or should I try to make a new Activity? – yanki Feb 06 '14 at 07:59
  • I have, it still throws me a NullPointerException at OnCreate() of ProjectsFragment – yanki Feb 06 '14 at 08:39