Android 将数组列表传递回父活动 [英] Android passing an arraylist back to parent activity

查看:26
本文介绍了Android 将数组列表传递回父活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个简单的例子,但没有运气.

I've been searching for a simple example of this with no luck.

在我的 android 应用程序中,我有两个活动:1. 启动时启动的主要活动2. 按下主活动上的按钮启动的第二个活动.

In my android application I have two activities: 1. The main activity which is launched at startup 2. A second activity which is launched by pressing a button on the main activty.

当第二个活动完成时(通过按下按钮),我希望它将 MyObject 类型的 ArrayList 发送回主活动并关闭自身,然后主活动可以对其进行任何操作.我将如何实现这一目标?我一直在尝试一些东西,但是当我开始第二个活动时它使我的应用程序崩溃.

When the second activity is finished (by pressing a button) I want it to send back an ArrayList of type MyObject to the main activity and close itself, which the main activity can then do whatever with it. How would I go about achieving this? I have been trying a few things but it is crashing my application when I start the second activity.

当用户按下按钮启动第二个活动时:

When the user presses button to launch second activity:

            Intent i = new Intent(MainActivity.this, secondactivity.class);
            startActivityForResult(i, 1);

在第二个活动上按下按钮后捆绑回来的数组:

The array which is bundled back after pressing a button on the second activity:

Intent intent= getIntent();
                    Bundle b = new Bundle();
                    b.putParcelableArrayList("myarraylist", mylist);
                    intent.putExtras(b);
                    setResult(RESULT_OK, intent);
                    finish();

最后是主要活动的侦听器(尽管我不确定此代码启动时是否为 100%...)

And finally a listener on the main activity (although I'm not sure of 100% when this code launches...)

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(resultCode==RESULT_OK && requestCode==1){
        Bundle extras = data.getExtras();

        final ArrayList<MyObject> mylist = extras.getParcelableArrayList("myarraylist");
        Toast.makeText(MainActivity.this, mylist.get(0).getName(), Toast.LENGTH_SHORT).show();
    }
}

任何想法我哪里出错了?onActivityResult() 似乎使我的应用程序崩溃.

Any ideas where I am going wrong? The onActivityResult() seems to be crashing my application.

具有可打包方法的类:

import android.os.Parcel;
import android.os.Parcelable;

public class Plan implements Parcelable{
    private String name;
    private String id;

    public Plan(){
    }

    public Plan createFromParcel(Parcel in) {
        Plan plan = new Plan();
        plan.setId(in.readString());
        plan.setName(in.readString());
        return plan;
}

    public Plan(String name, String id){
        this.name = name;
        this.id = id;
    }


    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }

    public String toString(){
        return "Plan ID: " + id + " Plan Name: " + name;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
    }

    public static final Parcelable.Creator<Plan> CREATOR
    = new Parcelable.Creator<Plan>() {
        public Plan createFromParcel(Parcel in) {
            return new Plan();
        }

        @Override
        public Plan[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Plan[size];
        }

    };

}

第二个活动完成后,onactivityresult 被调用,但是 toast 里面什么也没有显示,它是空白的.有任何想法吗?我猜我的课还是乱七八糟的...

After the second activity finished, onactivityresult is called, but nothing displays inside the toast, its blank. any ideas? I'm guessing my class is still messed up...

让它工作

我在错误的地方使用了彼得提供的方法.它应该在创作者内部,像这样:

I had the method that Peter supplied in the wrong place. It should be inside creator, like this:

public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
    public Plan createFromParcel(Parcel in) {
        Plan plan = new Plan();
        plan.setId(in.readString());
        plan.setName(in.readString());
        return plan;
}

而不是自己出去.

非常感谢彼得!希望这对其他人有帮助.

Many thanks to Peter! Hope this helps someone else.

推荐答案

你的类 MyObject 必须实现 Parcelable 以便在放入 Bundle 时被 Android 序列化/反序列化.

Your class MyObject must implement Parcelable in order to be serialized/deserialized by Android when put inside Bundle.

更新:

方法名称是createFromParcel.实际上,您必须根据包裹中的数据创建对象计划:

The method name is createFromParcel. And actually you have to create your object Plan from data in the parcel:

public Plan createFromParcel(Parcel in) {
        Plan plan = new Plan();
        plan.setId(in.readString());
        plan.setName(in.readString());
        return plan;
}

这篇关于Android 将数组列表传递回父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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