【问题标题】:Passing ArrayList through Intent通过 Intent 传递 ArrayList
【发布时间】:2011-07-19 11:23:12
【问题描述】:

我正在尝试使用意图将 arrayList 传递给另一个活动。这是第一个活动中的代码。

case R.id.editButton:
        Toast.makeText(this, "edit was clicked", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, editList.class);
        intent.putStringArrayListExtra("stock_list", stock_list);
        startActivity(intent);
        break;

这是我尝试在第二个活动中检索列表的地方。这里有什么问题吗?

Intent i = new Intent(); //This should be getIntent();
    stock_list = new ArrayList<String>();

    stock_list = i.getStringArrayListExtra("stock_list");

【问题讨论】:

    标签: android arraylist android-intent


    【解决方案1】:

    在你的接收意图中你需要做:

    Intent i = getIntent();  
    stock_list = i.getStringArrayListExtra("stock_list");
    

    按照您的方式,您刚刚创建了一个新的空意图,没有任何额外内容。

    如果你只有一个额外的,你可以把它浓缩成:

    stock_list = getIntent().getStringArrayListExtra("stock_list");
    

    【讨论】:

      【解决方案2】:

      我通过 String 的形式传递 ArrayList 来完成此操作。

      1. dependenciesbuild.gradle 中添加 compile 'com.google.code.gson:gson:2.2.4'

      2. 点击将项目与 Gradle 文件同步

      Cars.java

      public class Cars {
          public String id, name;
      }
      

      FirstActivity.java

      当你想要通过 ArrayList

      List<Cars> cars= new ArrayList<Cars>();
      cars.add(getCarModel("1", "A"));
      cars.add(getCarModel("2", "B"));
      cars.add(getCarModel("3", "C"));
      cars.add(getCarModel("4", "D"));
      
      Gson gson = new Gson();
      
      String jsonCars = gson.toJson(cars);
      
      Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
      intent.putExtra("list_as_string", jsonCars);
      startActivity(intent);
      

      通过Function获取CarsModel

      private Cars getCarModel(String id, String name){
             Cars cars = new Cars();
             cars.id = id;
             cars.name = name;
          return cars;
       }
      

      SecondActivity.java

      你必须导入java.lang.reflect.Type;

      onCreate() 上检索 ArrayList

      String carListAsString = getIntent().getStringExtra("list_as_string");
      
      Gson gson = new Gson();
      Type type = new TypeToken<List<Cars>>(){}.getType();
      List<Cars> carsList = gson.fromJson(carListAsString, type);
      for (Cars cars : carsList){
         Log.i("Car Data", cars.id+"-"+cars.name);
      }
      

      希望这会节省时间,我保存了。

      完成

      【讨论】:

      • 或者你可以简单地让你的汽车模型类实现 Parcelable 接口,然后简单地做 intent.putParcelableArrayListExtra("parcelable_list", carsList);其中 carsList 是 ArrayList 的一个实例
      • 反对者应该注意添加评论作为理由。
      • 如果已经存在intent.putStringArrayListExtraintent.getStringArrayListExtragetSerializableExtra,为什么还要使用Gson。如果我使用 Eclipse 怎么办?我必须下载并添加 Gson 库?
      • 我的 2 位...因此,您不能对此投反对票,但您应该推荐 Parceling over Serializing,因为它已被多次证明 - 对于 Android 而言,它更高效且执行序列化。
      【解决方案3】:

      如果您使用 Generic Array ListClass 而不是 specific type

      前:

      private ArrayList<Model> aListModel = new ArrayList<Model>();
      

      这里,Model = Class

      接收意图:

      aListModel = (ArrayList<Model>) getIntent().getSerializableExtra(KEY);
      

      必须记住:

      这里的模型类必须像这样实现: ModelClass 实现 Serializable

      【讨论】:

      • 通常类似于为 startActivityForResult 发送 Intent:Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, aListModel);
      【解决方案4】:

      假设你需要从当前活动传递一个跟随类的数组列表到下一个活动 // 数组列表中对象的类 // 记得从 Serializable 接口实现类 // 可序列化意味着它将对象转换为字节流并帮助传输该对象

      public class Question implements Serializable {
      ... 
      ... 
      ...
      }
      

      在您当前的活动中,您可能有一个如下所示的 ArrayList

      ArrayList<Question> qsList = new ArrayList<>();
      qsList.add(new Question(1));
      qsList.add(new Question(2));
      qsList.add(new Question(3));
      
      // intialize Bundle instance
      Bundle b = new Bundle();
      // putting questions list into the bundle .. as key value pair.
      // so you can retrieve the arrayList with this key
      b.putSerializable("questions", (Serializable) qsList);
      Intent i = new Intent(CurrentActivity.this, NextActivity.class);
      i.putExtras(b);
      startActivity(i);
      

      为了在下一个活动中获取数组列表

      //get the bundle
      Bundle b = getIntent().getExtras();
      //getting the arraylist from the key
      ArrayList<Question> q = (ArrayList<Question>) b.getSerializable("questions");
      

      【讨论】:

        【解决方案5】:
        //arraylist/Pojo you can Pass using bundle  like this 
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        Bundle args = new Bundle();
                                args.putSerializable("imageSliders",(Serializable)allStoriesPojo.getImageSliderPojos());
                                intent.putExtra("BUNDLE",args);
         startActivity(intent); 
        
        
        Get SecondActivity like this
          Intent intent = getIntent();
                Bundle args = intent.getBundleExtra("BUNDLE");
        String filter = bundle.getString("imageSliders");
        
        //Happy coding
        

        【讨论】:

        • 它正在抛出异常:java.lang.ClassCastException: com.example.ModelObjectNameHere cannot be cast to java.io.Serializable
        【解决方案6】:
            public class StructMain implements Serializable {
            public  int id;
            public String name;
            public String lastName;
        }
        

        这是我的项目。实现可序列化 并创建 ArrayList

        ArrayList<StructMain> items =new ArrayList<>();
        

        并放入捆绑包

        Bundle bundle=new Bundle();
        bundle.putSerializable("test",items);
        

        并创建一个将Bundle放入Intent的新Intent

        Intent intent=new Intent(ActivityOne.this,ActivityTwo.class);
        intent.putExtras(bundle);
        startActivity(intent);
        

        为接收捆绑插入此代码

        Bundle bundle = getIntent().getExtras();
        ArrayList<StructMain> item = (ArrayList<StructMain>) bundle.getSerializable("test");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-24
          相关资源
          最近更新 更多