Pass ArrayList<? implements Parcelable> to Acti

2019-01-08 17:32发布

问题:

I have searched a few topics but not found a solution to my problem.

public class Series implements Parcelable {
private String name;
private int numOfSeason;
private int numOfEpisode;

/** Constructors and Getters/Setters have been removed to make reading easier **/

public Series(Parcel in) {
    String[] data = new String[3];
    in.readStringArray(data);
    this.name = data[0];
    this.numOfSeason = Integer.parseInt(data[1]);
    this.numOfEpisode = Integer.parseInt(data[2]);
}


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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeStringArray(new String[] { this.name,
            String.valueOf(this.numOfSeason),
            String.valueOf(this.numOfEpisode) });

}

private void readFromParcel(Parcel in) {
    name = in.readString();
    numOfSeason = in.readInt();
    numOfEpisode = in.readInt();
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    @Override
    public Series createFromParcel(Parcel in) {
        return new Series(in);
    }

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

}

In my MainActivity I have an ArrayList. To make the list dynamically editeable I need to pass it to another activity where I can edit it.

ArrayList<Series> listOfSeries = new ArrayList<Series>();

    public void openAddActivity() {
    Intent intent = new Intent(this, AddActivity.class);
    intent.putParcelableArrayListExtra(
            "com.example.episodetracker.listofseries",
            (ArrayList<? extends Parcelable>) listOfSeries);
    startActivity(intent);
}

I need to cast the list, otherwise Eclipse gives me the following Error message. The method putParcelableArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, List)

Is this the correct way to do it?

    ArrayList<Series> list = savedInstanceState
            .getParcelableArrayList("com.example.episodetracker.listofseries");

This is the way I try to read the data in another activity.

It's crashing on the line above. namely the getParcelableArrayList part.

回答1:

  • The problem is in writing out to the parcel and reading in from the parcel ...

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(numOfSeason);
        dest.writeInt(numOfEpisode);
    }
    
    private void readFromParcel(Parcel in) {
        name = in.readString();
        numOfSeason = in.readInt();
        numOfEpisode = in.readInt();
    }
    
  • What you write out has to match what you read in...

    @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Intent i = new Intent(this,SecondActivity.class);
    
    ArrayList<testparcel> testing = new ArrayList<testparcel>();
    
    i.putParcelableArrayListExtra("extraextra", testing);
    startActivity(i);
    }
    
        /**********************************************/
    
    
    public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
     }
    }
    
  • The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.



回答2:

You should use the putParcelableArrayListExtra() method on the Intent class.



回答3:

I've used putParcelableArrayList(<? extends Parcelable>) from a Bundle Object. Not directly from an Intent Object.(I don't really know what's the difference). but i use to use in this way:

ArrayList<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
resultSet = loadData();

Bundle data = new Bundle();
data.putParcelableArrayList("search.resultSet", resultSet);
yourIntent.putExtra("result.content", data);
startActivity(yourIntent);

Later on your new activity you can populate the data recently inserted on the Bundle object like this:

Bundle data = this.getIntent().getBundleExtra("result.content");
ArrayList<ParcelableRow> result = data.getParcelableArrayList("search.resultset");

Just remember that your ArrayList<> must contain only parcelable objects. and just to make sure that your have passed the data you may check if the data received is null or not, just to avoid issues.



回答4:

Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth



回答5:

I am doing it in this way:

var intent = Intent(this@McqActivity,ResultActivity::class.java)
intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList)) 
// resultList  is of type mutableListOf<ResultBO>()  
 startActivity(intent)

And for making the class Parcelable . I simply do two operation first I used @Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..

import kotlinx.android.parcel.Parcelize
import android.os.Parcelable

@Parcelize // Include Annotion 
data class ResultBO(val questionBO: QuestionBO) : Parcelable {
    constructor() : this(QuestionBO())
}

And at receiving end

if (intent != null) {
     var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
}