Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Passing ArrayList<Objects> to another Activity

Posted on 2012-04-08
4
Medium Priority
?
5,852 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
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…

618 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