Solved

Passing ArrayList<Objects> to another Activity

Posted on 2012-04-08
4
5,511 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 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: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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Xiaomi and MIUI 7 139
Android app that calculates age from calendar and facebook 3 54
spam sms 9 35
Measure and control tethering schedule 2 29
This is an explanation of a simple data model to help parse a JSON feed
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

770 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