Help passing an ArrayList of Objects to a new Activity

前端 未结 10 2157
眼角桃花
眼角桃花 2020-11-28 08:53

I have an arraylist of objects. ie ArrayList.

I want to pass this to a new Activity. I tried to use putParcelableArrayList but it has issues with the object. I remov

相关标签:
10条回答
  • 2020-11-28 09:19

    You have two options:

    • Objects in the ArrayList put implement Parceable as required by the method putParcelableArrayList ()

    • Or the objects can implement Serializable and use the method putSerializable() to add the ArrayList, ie bundle.putSerializable("arraylist", arraylist);

    Android passes Object in Bundle by serializing and deserializing (via Serializable or Parcelable). So all of your Objects you want to pass in a Bundle must implement one of those two interfaces.

    Most Object serialize easily with Serializable and it is much simpler to implement Serializable and add a static UID to the class than to write all the methods/classes required for Parcelable.

    0 讨论(0)
  • 2020-11-28 09:22

    Your object class should implement parcelable. The code below should get you started.

        public class ObjectName implements Parcelable {
    
        // Your existing code
    
            public ObjectName(Parcel in) {
                super(); 
                readFromParcel(in);
            }
    
            public static final Parcelable.Creator<ObjectName> CREATOR = new Parcelable.Creator<ObjectName>() {
                public ObjectName createFromParcel(Parcel in) {
                    return new ObjectName(in);
                }
    
                public ObjectName[] newArray(int size) {
    
                    return new ObjectName[size];
                }
    
            };
    
            public void readFromParcel(Parcel in) {
              Value1 = in.readInt();
              Value2 = in.readInt();
              Value3 = in.readInt();
    
            }
            public int describeContents() {
                return 0;
            }
    
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeInt(Value1);
                dest.writeInt(Value2);  
                dest.writeInt(Value3);
           }
        }
    

    To use the above do this:

    In 'sending' activity use:

    ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();  
    Bundle bundle = new Bundle();  
    bundle.putParcelableArrayList("arraylist", arraylist);
    

    In 'receiving' activity use:

    Bundle extras = getIntent().getExtras();  
    ArrayList<ObjectName> arraylist  = extras.getParcelableArrayList("arraylist");  
    ObjectName object1 = arrayList[0];
    

    and so on.

    0 讨论(0)
  • 2020-11-28 09:28

    You can use a public static field in some of your classes.

    See here for more advices.

    0 讨论(0)
  • 2020-11-28 09:29

    You can pass your arraylist using Serializable interface. (Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.)

    public class ObjectName implements Serializable{
        private int Value1;
        private int Value2;
        private int Value3;
    
        public ObjectName (int pValue1, int pValue2, int Value3) {
            Value1 = pValue1;
            Value2 = pValue2;
            Value3 = pValue3;
        }
    
        // Get Statements for each value below
        public int getValue1() {
            return Value1;
        } 
        // etc
    }
    

    Now in your current activity

    // here is the arrayList of yours
    ArrayList<ObjectName> objNamesList = new ArrayList<>();
    objNamesList.add(new ObjectName(1,1,1)); 
    objNamesList.add(new ObjectName(2,2,2)); 
    //intialize bundle instance
    Bundle b = new Bundle();
    //adding data into the bundle 
    //key => objNames referece for the arrayList
    //value => objNamesList , name of the arrayList 
    //cast the arraylist into Serializable 
    b.putSerializable("objNames", (Serializable) objNamesList);
    //init the intent
    Intent i = new Intent(CurrentActivity.this, NextActivity.class);
    i.putExtras(b);
    startActivity(i);
    

    at next activity you will need to do as follows to get the arraylist

    public class NextActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next_activity);
            // get the bundle
            Bundle b = getIntent().getExtras();
            ArrayList<ObjectName> q = (ArrayList<ObjectName>) b.getSerializable("objNames");
        }
    }
    
    0 讨论(0)
提交回复
热议问题