Solved

Passing ArrayList<Objects> to another Activity

Posted on 2012-04-08
4
5,387 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:santhanasamy
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:
santhanasamy earned 500 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:santhanasamy
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

First off, some of you reading this are already thinking there is an error in the title. It really should be Kindle Fire versus OTHER Android tablets. That's because the Amazon Kindle Fire IS an Android tablet. Well, it is and it's not. It's running…
I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now