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

Nam*_*lay 11 android listview arraylist android-intent

如何通过集合就像ArrayList从一个,等Activity另一个是我们用来传递Strings,int意图的putExtra方法的帮助?

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

nha*_*man 31

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

你可以调用putExtra (String name, Serializable value)of Intent来存储和getSerializableExtra (String name)检索.

例:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
Run Code Online (Sandbox Code Playgroud)

在其他活动中:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Run Code Online (Sandbox Code Playgroud)

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

  • 值得一提的是,`List`接口不扩展`Serializable`所以如果你的集合是由接口声明的,你必须先将它转换为特定的实现. (6认同)
  • +1提到E必须是Serializable,其他人似乎忘了提及. (3认同)
  • 注意:- intent.putExtra("mylist", (Serializable)myList); 为我工作! (2认同)

Sha*_*amS 16

首先,您需要创建一个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];
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

和清单

ArrayList<Student> arraylist = new ArrayList<Student>();
Run Code Online (Sandbox Code Playgroud)

来自呼叫活动的代码

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

被叫活动的代码

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

    Bundle bundle = getIntent().getExtras();
    ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*ddy 5

用于putExtra将值传递给意图。使用getSerializableExtra这样的方法来检索数据

活动一:

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

intent.putExtra("arraylist", list);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

活动B:

ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
Run Code Online (Sandbox Code Playgroud)


小智 5

我尝试了所有建议的技术,但它们都不起作用,并阻止了我的应用程序工作,然后我终于成功了。这是我是如何做到的......在主要活动中,我是这样做的:

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

获取新活动中的数据:

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

我希望这会帮助某人...