?
Solved

Passing ArrayList<Objects> to another Activity

Posted on 2012-04-08
4
Medium Priority
?
6,282 Views
Last Modified: 2012-04-09
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
0
Comment
Question by:InfoTechEE
  • 3
4 Comments
 
LVL 8

Expert Comment

by:Santhana
ID: 37822308
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>
0
 

Author Comment

by:InfoTechEE
ID: 37822338
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
            
      }

}
0
 
LVL 8

Accepted Solution

by:
Santhana earned 2000 total points
ID: 37822373
Hi  InfoTechEE,

If you pass ArrayList of String it will not give any error... Because internally this String class implements  parcelable...

Refer android docs  http://developer.android.com/reference/android/os/Parcelable.html

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

public class FileInfo implements Parcelable {

private String prevName = "";
private String destinationFileName = "";

public FileInfo(Parcel source){
prevName = source.readString()
destinationFileName = source.readString();
}
public FileInfo(){
}

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 dest, int arg1) {
      dest.writeString(prevName );
      dest.writeString(destinationFileName );
}
}

public static final Parcelable.Creator<FileInfo > CREATOR  =  new Parcelable.Creator<FileInfo >() {
      public FileInfo createFromParcel(Parcel in) {
            return new FileInfo (in);
      }
      public FileInfo [] newArray(int size) {
            return new FileInfo [size];
      }
};
}
0
 
LVL 8

Expert Comment

by:Santhana
ID: 37822380
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

These days software publishers make it possible to move all the possible applications on smartphones, tablets, smartwatches, and more. Although behind every logic and decision is a gross doubt: how do they make it possible? In this blog post, we sha…
In this article, I explain everything about the complete journey of a mobile app and its development lifecycle. Read the complete article to learn more about this fascinating topic.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question