0

i have an objects in an custom arraylist as "finaljsoncontent", and now i am trying to pass this "finaljsoncontent" array to another Activity, and i have also tried getters and setters, and also bundle, but i cant, help me how to do this. Thanks in advance.

  • ArrayList jsonArrayList = new ArrayList(); jsonKey.year_rate = object.getString(yearArray[i]+"_year_rate"); jsonKey.year_pi = object.getString(yearArray[i]+"_year_pi"); jsonKey.year_apr = object.getString(yearArray[i]+"_year_apr"); jsonArrayList.add(jsonKey); how to pass this jsonArrayList from one intent to another Intent, i came across Parcelable , and i dont have idea about using this Parcelable. help me to do this. – user1531843 Jul 31 '12 at 16:42

3 Answers3

0

You could try implementing Parcelable, then you can pass it in a bundle. You will need to reduce your object to mostly primitive types to do this. Otherwise you can extend the Application class and store it there. You would retrieve that using the call to getApplicationContext(). Or, of course, you could always create some sort of static globals class that all of your classes can reference.

Here is one of my implementations of parcelable..

package warrior.mail.namespace;

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

public class JView implements Parcelable {
    public String subject;
    public String from;
    public boolean unread;
    public String body;
    public int inboxIndex;
    private long id;
    public static final Parcelable.Creator<JView> CREATOR = new Parcelable.Creator<JView>() {

        public JView createFromParcel(Parcel in) {
            return new JView(in);
        }

        public JView[] newArray(int size) {
            return new JView[size];
        }

    };

    public JView(){
        body = "";
    }

    public JView(String subject,String from,boolean unread){
        body = "";
        this.subject = subject;
        this.from = from;
        this.unread = unread;
    }

    public JView(Parcel parcel){
        subject = parcel.readString();
        from = parcel.readString();
        body = parcel.readString();
        unread = parcel.createBooleanArray()[0];
        inboxIndex = parcel.readInt();
    }

    @Override
    public int describeContents() {
        return inboxIndex;
    }

    @Override
    public void writeToParcel(Parcel out, int arg1) {
        out.writeString(subject);
        out.writeString(from);
        out.writeString(body);
        boolean[] array = new boolean[] {unread};
        out.writeBooleanArray(array);
        out.writeInt(inboxIndex);
    }

    public void setIndex(int index){
        inboxIndex = index;
    }

    public void setUnread(boolean arg){
        unread = arg;
    }

    public void setContent(String content){
        body = content;
    }

    public void setSubject(String subject){
        this.subject = subject;
    }

    public void setFrom(String f){
        from = f;
    }

    public void setId(long arg){
        id = arg;
    }

    public long getId(){
        return id;
    }

    public void updateIndex(){

    }
}
Joel
  • 4,732
  • 9
  • 39
  • 54
  • i am looking into Parcelable for the past 2 hours, im not getting expected output, kindly help me out with a sample for the above requirment. Thanks – user1531843 Jul 31 '12 at 16:45
  • Which option would you like an example for? – Joel Jul 31 '12 at 16:48
  • public class JsonKey { public String year_pi; public String year_apr; public String year_rate; } This is the Class. ArrayList jsonArrayList = new ArrayList(); jsonKey.year_rate = object.getString(yearArray[i]+"_year_rate"); jsonKey.year_pi = object.getString(yearArray[i]+"_year_pi"); jsonKey.year_apr = object.getString(yearArray[i]+"_year_apr"); jsonArrayList.add(jsonKey); I need to take this arraylist to next activity through intent. – user1531843 Jul 31 '12 at 16:53
  • 1
    Ok, two things. 1. Put your code in the question, not in a comment. No one want's to try and read that. 2. You cannot expect us to write the code you need to do this. We can point you in the right direction, or show you an implementation, but it is your job to do your project. Research, ask, learn, and then do. How can you learn without doing it yourself? Look at my example, and apply it to your code. If you're having trouble, post your code up top so we can take a look. – Joel Jul 31 '12 at 16:57
  • If the class contains just strings, and you really are stuck, you could just make new ArrayLists for the 3 string objects in JsonKey, bundle them and rebuild the JsonKey on the other side. Not an ideal solution... –  Jul 31 '12 at 16:58
0

Check this out: How do I pass an object from one activity to another on Android?

Your class "JSonKey" should implement parcealable or serializable so that Android can "send" it from an activity to the other activity.

Community
  • 1
  • 1
luanjot
  • 1,176
  • 1
  • 7
  • 21
0

You can either make your class Parcelable(android specific) or make it serializable like in java(just write implements Serializable with your class)

sachy
  • 739
  • 7
  • 13