如何将类的arraylist从一个活动传递到另一个活动onclick适配器Textview

时间:2016-08-22 09:53:41

标签: android

这个我的类实现了一个textview的Serializable和onclick我正在将arraylist从一个活动传递到其他活动,但在检索那里有一些问题时请帮助,如果有人知道这类问题。

public class RoomCategoryList implements Serializable {
        @SerializedName("roomCode")
        @Expose
        private String roomCode;
        @SerializedName("roomCategoryCode")
        @Expose
        private String roomCategoryCode;
        @SerializedName("roomCategoryName")
        @Expose
        private String roomCategoryName;
        @SerializedName("roomType")
        @Expose
        private String roomType;
        @SerializedName("boardData")
        @Expose
        private BoardData boardData;
        @SerializedName("roomImages")
        @Expose
        private List<String> roomImages = new ArrayList<String>();
        @SerializedName("roomAllotment")
        @Expose
        private int roomAllotment;
        @SerializedName("listPrice")
        @Expose
        private double listPrice;
        @SerializedName("offerPrice")
        @Expose
        private double offerPrice;
        @SerializedName("refundable")
        @Expose
        private boolean refundable;
        @SerializedName("roomCategoryAdjustment")
        @Expose
        private int roomCategoryAdjustment;
        @SerializedName("cancelPolicy")
        @Expose
        private List<CancelPolicy> cancelPolicy = new ArrayList<CancelPolicy>();
        @SerializedName("bedBankAdditionalData")
        @Expose
        private String bedBankAdditionalData;
        @SerializedName("hotelInformations")
        @Expose
        private List<HotelInformation> hotelInformations = new ArrayList<HotelInformation>();
        @SerializedName("facilities")
        @Expose
        private List<Facility> facilities = new ArrayList<Facility>();
        @SerializedName("currency")
        @Expose
        private String currency;

        public String getRoomCode() {
            return roomCode;
        }

        public void setRoomCode(String roomCode) {
            this.roomCode = roomCode;
        }

        public String getRoomCategoryCode() {
            return roomCategoryCode;
        }

        public void setRoomCategoryCode(String roomCategoryCode) {
            this.roomCategoryCode = roomCategoryCode;
        }

        public String getRoomCategoryName() {
            return roomCategoryName;
        }

        public void setRoomCategoryName(String roomCategoryName) {
            this.roomCategoryName = roomCategoryName;
        }

        public String getRoomType() {
            return roomType;
        }

        public void setRoomType(String roomType) {
            this.roomType = roomType;
        }

        public BoardData getBoardData() {
            return boardData;
        }

        public void setBoardData(BoardData boardData) {
            this.boardData = boardData;
        }

        public List<String> getRoomImages() {
            return roomImages;
        }

        public void setRoomImages(List<String> roomImages) {
            this.roomImages = roomImages;
        }

        public int getRoomAllotment() {
            return roomAllotment;
        }

        public void setRoomAllotment(int roomAllotment) {
            this.roomAllotment = roomAllotment;
        }

        public double getOfferPrice() {
            return offerPrice;
        }

        public void setOfferPrice(double offerPrice) {
            this.offerPrice = offerPrice;
        }

        public double getListPrice() {
            return listPrice;
        }

        public void setListPrice(double listPrice) {
            this.listPrice = listPrice;
        }

        public boolean isRefundable() {
            return refundable;
        }

        public void setRefundable(boolean refundable) {
            this.refundable = refundable;
        }

        public int getRoomCategoryAdjustment() {
            return roomCategoryAdjustment;
        }

        public void setRoomCategoryAdjustment(int roomCategoryAdjustment) {
            this.roomCategoryAdjustment = roomCategoryAdjustment;
        }

        public List<CancelPolicy> getCancelPolicy() {
            return cancelPolicy;
        }

        public void setCancelPolicy(List<CancelPolicy> cancelPolicy) {
            this.cancelPolicy = cancelPolicy;
        }

        public String getBedBankAdditionalData() {
            return bedBankAdditionalData;
        }

        public void setBedBankAdditionalData(String bedBankAdditionalData) {
            this.bedBankAdditionalData = bedBankAdditionalData;
        }

        public List<HotelInformation> getHotelInformations() {
            return hotelInformations;
        }

        public void setHotelInformations(List<HotelInformation> hotelInformations) {
            this.hotelInformations = hotelInformations;
        }

        public List<Facility> getFacilities() {
            return facilities;
        }

        public void setFacilities(List<Facility> facilities) {
            this.facilities = facilities;
        }

        public String getCurrency() {
            return currency;
        }

        public void setCurrency(String currency) {
            this.currency = currency;
        }
    } 

我想将这个arraylist发送到另一个活动,任何帮助

2 个答案:

答案 0 :(得分:1)

For passing Arraylist from one activity A to another activity B, you can refer following code.

In Activity A: 
Intent intent = new Intent(mContext, YourActivity.class);
Bundle bundleList = new Bundle();
        bundleList.putSerializable("your string", (Serializable) mArrayList);
intent.putExtras(bundleList);

In Activity B:
Intent intent = getIntent();`enter code here`
ArrayList<String> list =  (ArrayList<String>)intent.getSerializableExtra("your string");

为了获得更好的性能,您应该使用Parceleable而不是Serializable

答案 1 :(得分:0)

如果E类型是Serializable,则可以以相同的方式传递ArrayList。

示例:

putExtra first Activity

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList)
第二个活动中的

getIntent:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");