将列表从一个活动传递到另一个活动

时间:2012-07-05 08:45:51

标签: android listview android-intent arraylist

如何将ArrayList等集合从一个Activity传递到另一个{I}我们曾经借助于Intent的putExtra方法传递Stringsint

任何人都可以帮助我,因为我想将List<String>从一个Activity传递给另一个人吗?

6 个答案:

答案 0 :(得分:28)

如果ArrayList<E>类型为E,您可以采用相同的方式传递Serializable

您可以调用putExtra (String name, Serializable value) Intent来存储,getSerializableExtra (String name)进行检索。

示例:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);

在另一项活动中:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");

请注意,序列化可能会导致性能问题:需要时间,并且会分配大量对象(因此必须进行垃圾回收)。

答案 1 :(得分:14)

首先,您需要创建一个Parcelable对象类,请参阅示例

public class Student implements Parcelable {

        int id;
        String name;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;

        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }


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

        @Override
        public void writeToParcel(Parcel dest, int arg1) {
            // TODO Auto-generated method stub
            dest.writeInt(id);
            dest.writeString(name);
        }

        public Student(Parcel in) {
            id = in.readInt();
            name = in.readString();
        }

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

            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    }

和列表

ArrayList<Student> arraylist = new ArrayList<Student>();

来自通话活动的代码

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);       
this.startActivity(intent);

被叫活动代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);   

    Bundle bundle = getIntent().getExtras();
    ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}

答案 2 :(得分:4)

使用putExtra将值传递给intent。使用getSerializableExtra方法检索数据 像这样

活动A:

ArrayList<String> list = new ArrayList<String>();

intent.putExtra("arraylist", list);
startActivity(intent);

活动B:

ArrayList<String> list = getIntent().getSerializableExtra("arraylist");

答案 3 :(得分:4)

我尝试了所有提议的技术,但没有一个工作,阻止我的应用程序工作,然后我终于成功了。我是这样做的...... 在主要活动中,我这样做了:

            List<String> myList...;
            Intent intent = new Intent...;
            Bundle b=new Bundle();
            b.putStringArrayList("KEY",(ArrayList<String>)myList);            
            intent_deviceList.putExtras(b);
            ....startActivity(intent);

要获取新活动中的数据:

    List<String> myList...
    Bundle b = getIntent().getExtras();
    if (b != null) {
        myList = bundle.getStringArrayList("KEY");
    }

我希望这能帮助别人......

答案 4 :(得分:0)

要将ArrayList从一个活动传递到另一个活动,应该包括

intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass

在开始活动之前。并在另一个活动中获取ArrayList

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList
                }

您将能够通过此传递ArrayList。希望这对您有所帮助。

答案 5 :(得分:0)

使用 ViewModel 比 Serializable 或 Parcelable 更好

相关问题