【问题标题】:How to pass sublist via intent如何通过意图传递子列表
【发布时间】:2018-08-27 10:13:41
【问题描述】:

我正在尝试通过Intenttop50TrendsList 传递给另一个活动,如下代码所示,但它被标记为红色,并且我收到一条错误消息:

2nd parameter cant be cast to Serializable

尽管Trend 类实现了Serializable 接口。

请告诉我如何通过Intenttop50TrendsList 传递给另一个活动?谢谢。

代码

    List<Trend> top50TrendsList = this.mTrendsList.subList(0, 2);
Collections.sort(this.mTrendsList);
    Intent intentSendBroadcast = new Intent();
    intentSendBroadcast.setAction(ActMain.CONST_BROADCAST_ACTION_ON_LIST_READY);
    Bundle b = new Bundle();
    b.putSerializable(TwitterTrendsAPIService.CONST_BUNDLE_KEY_SERIALIZED_LIST, top50TrendsList);
    intentSendBroadcast.putExtras(b);
    sendBroadcast(intentSendBroadcast);

【问题讨论】:

    标签: android list android-intent sublist


    【解决方案1】:

    那么你的Trend 应该是Parcelable,然后你就可以这样做了

    intent.putParcelableArrayListExtra("myArrayList" , top50TrendsList);
    

    查看this 以了解parceable,以及如何制作您的课程parceable

    希望这会有所帮助。

    【讨论】:

    • 关键是提供的是 List 对象,而不是趋势
    • @AndreyIlyunin 哦,伙计,认真的吗?你的列表对象top50TrendsList 的类型是Trend,你应该知道
    • 列表对象的类型是...列表
    • 他正在通过……那个名单:)
    • 和 List 接口没有实现可序列化。如果他传递 Trend 对象,而不是 Trend 的列表(通知,而不是 ArrayList)(实际上与哪个对象无关)对象,则此答案将很有用
    【解决方案2】:

    ArrayList 实现 Serializable,而不是 List。您通过 List(接口)引用提供对象(并且 Java 具有单一调度,因此要调用的重载方法变体在编译时由引用类型标识,而不是运行时的实际对象类型)。通过 ArrayList 参考传递它,我想这对你的目标来说是可以的。

    【讨论】:

      【解决方案3】:

      使用 Parcelable

      public class Test implements Parcelable
      {
          List<String> list;
      
          protected Test(Parcel in) {
              list = in.createStringArrayList();
          }
      
          public Test()
          {}
      
          public static final Creator<Test> CREATOR = new Creator<Test>() {
              @Override
              public Test createFromParcel(Parcel in) {
                  return new Test(in);
              }
      
              @Override
              public Test[] newArray(int size) {
                  return new Test[size];
              }
          };
      
          public List<String> getList() {
              return list;
          }
      
          public void setList(List<String> list) {
              this.list = list;
          }
      
          @Override
          public int describeContents() {
              return 0;
          }
      
          @Override
          public void writeToParcel(Parcel dest, int flags) {
              dest.writeStringList(list);
          }
      }
      

      在模型类上设置列表并传递给另一个活动:

      List<String> list = new ArrayList<>();
              Test test = new Test();
              test.setList(list);
      
              Intent intent = new Intent(this, Main2Activity.class);
              intent.putExtra("list", test);
              startActivity(intent);
      

      从第二个活动中获取数据:

      if (getIntent().hasExtra("list"))
              {
                  List<String> list = new ArrayList<>();
                  Test test = getIntent().getParcelableExtra("list");
                  list.addAll(test.getList());
              }
      

      【讨论】:

        【解决方案4】:

        你的 Trend 类应该像这样实现 Serializable

        公共类趋势实现 可序列化

        【讨论】:

          猜你喜欢
          • 2015-05-30
          • 2022-06-11
          • 2014-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-13
          • 1970-01-01
          • 2013-11-13
          相关资源
          最近更新 更多