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

查看:26
本文介绍了通过 ArrayList<?实现 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.为了使列表可动态编辑,我需要将它传递给另一个我可以编辑它的活动.

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 会给我以下错误消息.Intent 类型中的 putParcelableArrayListExtra(String, ArrayList) 方法不适用于参数 (String, List)

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)

这是正确的做法吗?

    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.

推荐答案

  • 问题在于向包裹写出并从包裹中读入...

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

  • 上面的代码有来自两个不同活动的 onCreate().第一个启动第二个;并且它工作正常我能够毫无问题地拉出parcelable.

  • 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.

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

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