简体   繁体   中英

How do I iterate through an ArrayList of custom objects from Intent and add them into LinearLayout?

I have an ArrayList of custom FlightData objects within the intent. I load the intent and get the arraylist as null, and the foreach loop also forces me to use Object as type.

Saving arraylist into intent:

intent.putParcelableArrayListExtra("FlightDataList", (ArrayList<? extends Parcelable>) flightDataList);

Loading of intent:

Intent intent = getIntent();
LinearLayout layout_datasheet =  findViewById(R.id.layout_datasheet);
List flightDataList = intent.getParcelableArrayListExtra("FlightDataList");

if (flightDataList == null){
    Log.d("flightDataList_size", "FlightDataList is null"); // this fires
}

assert flightDataList != null;
for (Object data : flightDataList){
    data = (FlightData) data; // items in list are of type FlightData
    TextView tv = new TextView(this);
    tv.setText(data.toString());
    layout_datasheet.addView(tv);
}

My custom class' parcelable functions (x,y,time, has getters-setters):

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(x);
        dest.writeDouble(y);
        dest.writeDouble(time);
    }

    public static final Creator<FlightData> CREATOR = new Creator<FlightData>() {
        @Override
        public FlightData createFromParcel(Parcel in) {
            return new FlightData(in);
        }

        @Override
        public FlightData[] newArray(int size) {
            return new FlightData[size];
        }
    };

1.First Implement Parceable in your FlightData object model / pojo / class

2. val flightDataList= ArrayList<FlightData>()

3. val args = Bundle()

4. args.putParcelableArrayList("FlightDataList", flightDataList)

5. intent.putExtra(args)

Then to get list

val flightDataList = context.getIntent().getExtras().getParcelableArrayList("FlightDataList")

did you try creating a datastructure that implements parcelable?

public class flightDataList implements Parcelable{
    String dataThingyString;
    int dataThingyInt;


    public flightDataList(String dataThingyString, int dataThingyInt){
        this.dataThingyString = dataThingyString;
        this.dataThingyInt = dataThingyInt;
    }

    public flightDataList(Parcle in){
        this.dataThingyString = in.readString();
        this.dataThingyInt = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags){
        dest.writeString(dataThingyString);
        dest.writeInt(dataThingyInt);
    }

    public static final Creator<flightDataList> CREATOR = new Creator<flightDataList>(){
        @Override
        public flightDataList createFromParcel(Parcel source){
            return new flightDataList(source);
        }

        @Override
        public flightDataList[] newArray(int size){
            return new flightDataList[size];
        }
    }

    public void setdataThingyString(String stringData){
        this.dataThingyString = stringData;
    }

    public void setdataThingyInt(int intData){
        this.dataThingyInt = intData;
    }

    public String getdataThingyString(){
        return dataThingyString;
    }

    public int getdataThingyInt(){
        return dataThingyInt;
    }

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

It should work. The only thing that I am missing in your example is the constructor. It could explain the null your are getting.

Try adding this constructor for FlightData

  public FlightData(Parcel in) {
    x = in.readDouble();
    y = in.readDouble();
    time  = in.readDouble();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM