Link to home
Start Free TrialLog in
Avatar of InfoTechEE
InfoTechEE

asked on

Passing ArrayList<Objects> to another Activity

I am trying to pass an ArrayList of Objects from one Activity to another. I am trying to implement the solution below, but getting an error message. Can someone please help out.

//to pass :
public ArrayList<FileInfo> audioFileArrayList = null;
audioFileArrayList = GetFileInfo(previewName, destFileName);

private ArrayList<FileInfo> GetFileInfo(String strPrevName, String strDestName) {
            ArrayList<FileInfo> file_info = new ArrayList<FileInfo>();
            FileInfo fi = new FileInfo();
            fi.setPrevName(strPrevName);
            fi.setDestinationFileName(strDestName);
            file_info.add(fi);
            
            return file_info;
      }

Intent i = new Intent(this, SavedAudio.class);
i.putParcelableArrayListExtra("ObjArray", (ArrayList<? extends Parcelable>) AudioFileArrayList);
startActivity(i);


// to retrieve object in second Activity
ArrayList<FileInfo> fileInfo = getIntent().getParcelableArrayListExtra("ObjArray");

Error Message:
Bound mismatch: The generic method getParcelableArrayListExtra(String) of type Intent is not applicable for the arguments (String). The inferred type FileInfo is not a valid substitute for the bounded parameter <T extends Parcelable>

Attached:
FileInfo Class
FileInfo.java
Avatar of Santhana
Santhana
Flag of India image

Hi  InfoTechEE,

From your Error Message it is clearly visible that  FileInfo object should be a Parcelable.
Bound mismatch: The generic method getParcelableArrayListExtra(String) of type Intent is not applicable for the arguments (String). The inferred type FileInfo is not a valid substitute for the bounded parameter <T extends Parcelable>
Avatar of InfoTechEE
InfoTechEE

ASKER

How do I modify the FileInfo class to be Parcelable?

I didn't realize it would be this difficult to pass an ArrayList from one Activity to another. It sounds like it should be very simple.

Here's a start, but I'm not sure what's suppose to be done here. The Android documentation doesn't make it any simpler either. Any help would be appreciated. Thank you.

import android.os.Parcel;
import android.os.Parcelable;

public class FileInfo implements Parcelable {
      
      private String prevName = "";
      private String destinationFileName = "";
      
      public void setPrevName(String previewName) {
            this.prevName = previewName;
      }
      
      public String getPrevName() {
            return prevName;
      }
      
      public void setDestinationFileName(String destFileName) {
            this.destinationFileName = destFileName;
      }
      
      public String getDestinationFileName() {
            return destinationFileName;
      }

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

      @Override
      public void writeToParcel(Parcel arg0, int arg1) {
            // TODO Auto-generated method stub
            
      }

}
ASKER CERTIFIED SOLUTION
Avatar of Santhana
Santhana
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi  InfoTechEE,

This is like an android way of doing interprocess communication....For that android  defind this data structure called Parcel...

This is simillar to the j2sdk Serialization....

Refer this link....for more details...
http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html