通过Intent发送ArrayList

5

如何通过 Intent 从另一个活动接收自定义的 ArrayList?例如,在 Activity A 中有这个 ArrayList

ArrayList<Song> songs;

我该如何在B活动中获取此列表?
5个回答

9
首先要了解的是,您可以使用一个 Intent 对象将信息从 Activity A 传递到 Activity B,并在其中放置“extras”。您可以在此处查看可放置在 Intent 中的完整列表:https://developer.android.com/reference/android/content/Intent.html(请参阅各种 putExtra() 方法以及下面的 putFooExtra() 方法)。
由于您正在尝试传递一个 ArrayList<Song>,因此您有两个选项。
第一个,也是最好的选择,是使用 putParcelableArrayListExtra()。为了使用它,Song 类必须实现 Parcelable 接口。如果您控制着 Song 的源代码,则实现 Parcelable 相对容易。您的代码可能如下所示:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putParcelableArrayListExtra("songs", songs);

第二种方法是使用接受 Serializable 对象的 putExtra() 版本。当你无法控制 Song 的源代码并因此无法实现 Parcelable 时,才应该使用此选项。你的代码可能如下所示:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putSerializableExtra("songs", songs);

那么这就是在 Activity A 中将数据放入 Intent 的方法。如何从 Activity B 中取出数据呢?
这取决于你之前选择的选项。如果你选择了第一种选项,你需要编写类似以下内容的代码:
List<Song> mySongs = getIntent().getParcelableArrayListExtra("songs");

如果您选择第二个选项,您将编写类似以下的内容:
List<Song> mySongs = (List<Song>) getIntent().getSerializableExtra("songs");

第一种技术的优点是它更快(从用户角度看),并且占用的空间更小(在传递数据的大小方面)。

3
Misam正在发送歌曲列表,因此不能使用普通的putStringArrayList()。相反,Song类必须实现Parcelable接口。我已经在这里的帖子中解释了如何轻松实现Parcelable。
实现Parcelable接口后,只需按照Uddhav的答案进行一些小修改即可。
// First activity, adding to bundle
bundle.putParcelableArrayListExtra("myArrayListKey", arrayList);

// Second activity, reading from bundle
ArrayList<Song> list = getIntent().getParcelableArrayListExtra("myArrayListKey");

3

希望这可以帮助您。

1. 您的Song类应实现Parcelable类

public class Song implements Parcelable { 
//Your setter and getter methods
}

2. 将您的ArrayList放入putParcelableArrayListExtra()中

public class ActivityA extends AppCompatActivity {
ArrayList<Song> songs;

@Override
protected void onCreate(Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), ActivityB.class)
            .putParcelableArrayListExtra("songs", (ArrayList<? extends Parcelable>) songs));
        }
    });
}

3. 在 ActivityB 中

public class ActivityB extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    final ArrayList<Song> songs = intent.getParcelableArrayListExtra("songs");

    //Check the value in the console
    buttonCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (Song value : songs) {
                System.out.println(value.getTitle());
            }
        }
    });
}

1

要在Java中发送一个字符串数组列表,你可以使用以下代码:

intent.putStringArrayListExtra("key", skillist <- your arraylist);

并且

 List<String> listName = getIntent().getStringArrayListExtra("key");

-1
请注意,bundle 是 Android 系统中用于组件间通信的关键组件之一。你所需要考虑的就是如何将你的数组放入该 bundle 中。 发送端(Activity A):
    Intent intent1 = new Intent(MainActivity.this, NextActivity.class);
    Bundle bundle = new Bundle();
    Parcelable[] arrayList = new Parcelable[10];

/* 注意:您必须使用writeToParcel()方法来写入您的歌曲对象的不同参数值 */
    /* you can add more string values in your arrayList */
    bundle.putParcelableArray("myParcelableArray", arrayList);        
    intent1.putExtra("myBundle", bundle);
    startActivity(intent1);

接收方(活动B)

        Bundle bundle2 = getIntent().getBundleExtra("myBundle"); /* you got the passsed bundle */
        Parcelable[] arrayList2 = bundle.getParcelableArray("myParcelableArray"); 

这不是字符串的ArrayList。 - Selvin
@Selvin,如果我创建ArrayList<T extends Object>,那么我无法通过Bundle发送它,因为它无法被序列化。因此,在这里我们无法泛化处理。 - Uddhav P. Gautam
那么,这并不是对这个问题的回答。 - Selvin
@Selvin,起初他的问题中没有包括ArrayList<Song>。 - Uddhav P. Gautam
@Selvin,这个答案现在不是吗? - Uddhav P. Gautam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接