通过ArrayList的&LT ;?实现Parcelable>到活动 [英] Pass ArrayList<? implements Parcelable> to Activity

查看:112
本文介绍了通过ArrayList的&LT ;?实现Parcelable>到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅了几个主题,但没有找到一个解决我的问题。

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];
    }
};

}

在我MainActivity我有一个ArrayList。为了使列表动态editeable我需要将它传递给另一个活动,我可以对其进行编辑。

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);
}

我需要转换列表,否则Eclipse中给了我下面的错误消息。
在类型意图的方法putParcelableArrayListExtra(字符串,ArrayList中)不适用的参数(字符串列表)

这是做了正确的方法是什么?

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.

它上面的崩溃就行了。即getParcelableArrayList一部分。

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

推荐答案

在万阿英,蒋达清是书面方式出去的包裹和包裹阅读...

The probelm is in writting 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");
    }
}

上面的code有两个工作之外的活动oncreates第一个启动第二个,它工作正常,我能够拉动parcelable没有问题。

The code above has oncreates from two activties the first one launches the second one and it works fine I was able to pull the parcelable without issue.

这篇关于通过ArrayList的&LT ;?实现Parcelable&GT;到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆