【问题标题】:send Arraylist by Intent按意图发送 Arraylist
【发布时间】:2017-08-01 19:45:02
【问题描述】:

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

ArrayList<Song> songs;

如何在活动 B 中获取此列表?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    在Java中发送一个可以使用的字符串arrayList,

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

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

    【讨论】:

      【解决方案2】:

      希望对你有帮助。

      1.您的 Song 类应该是 Parcelable 类的实现

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

      2。将您的数组列表放入 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());
                  }
              }
          });
      }
      

      【讨论】:

        【解决方案3】:

        首先要了解的是,您使用Intent 对象将信息从活动A 传递到活动B,您可以在其中放置“附加”。您可以在 Intent 中放入的内容的完整列表可在此处获得:https://developer.android.com/reference/android/content/Intent.html(请参阅各种 putExtra() 方法,以及下面的 putFooExtra() 方法)。

        由于您尝试传递ArrayList&lt;Song&gt;,因此您有两个选择。

        第一个也是最好的方法是使用putParcelableArrayListExtra()。要使用它,Song 类必须实现 Parcelable 接口。如果控制Song的源码,实现Parcelable相对容易。您的代码可能如下所示:

        Intent intent = new Intent(ActivityA.this, ActivityB.class);
        intent.putParcelableArrayListExtra("songs", songs);
        

        第二种是使用version of putExtra() that accepts a Serializable object。只有当您不控制Song 的源代码时才应使用此选项,因此无法实现Parcelable。您的代码可能如下所示:

        Intent intent = new Intent(ActivityA.this, ActivityB.class);
        intent.putSerializableExtra("songs", songs);
        

        这就是在活动 A 中将数据放入 Intent 的方式。如何从活动 B 中的 Intent 中取出数据?

        这取决于您在上面选择的选项。如果您选择第一个,您将编写如下所示的内容:

        List<Song> mySongs = getIntent().getParcelableArrayListExtra("songs");
        

        如果你选择第二个,你会写出这样的东西:

        List<Song> mySongs = (List<Song>) getIntent().getSerializableExtra("songs");
        

        第一种技术的优点是速度更快(就您的应用对用户而言的性能而言)并且占用的空间更少(就您传递的数据大小而言)。

        【讨论】:

          【解决方案4】:

          Misam 正在发送歌曲列表,因此它不能使用普通的putStringArrayList()。相反,Song 类必须实现 Parcelable 接口。我已经在here 中解释了如何实现 Parcelable 无痛。

          实现 Parcelable 接口后,只需按照 Uddhavs 的回答稍作修改即可:

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

          【讨论】:

            【解决方案5】:

            请注意, bundle 是 Android 系统中用于组件间通信的关键组件之一。您所需要考虑的就是如何使用将您的 Array 放入该捆绑包中。

            发送方(活动 A)

                Intent intent1 = new Intent(MainActivity.this, NextActivity.class);
                Bundle bundle = new Bundle();
                Parcelable[] arrayList = new Parcelable[10];
            

            /* 注意:你必须使用 writeToParcel() 方法来写入你的 Song 对象的不同参数值 */

                /* 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,如果我制作 ArrayList 那么我不能通过 Bundle 发送它,因为这不能打包。所以,我们不能在这里一概而论。
            • 那么这不是这个问题的答案。
            • @Selvin,起初他的问题中没有包含 ArrayList
            • @Selvin,现在不是这个答案吗?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多