Android 如何在两个片段之间传递JSON数据?

Android 如何在两个片段之间传递JSON数据?,android,android-fragments,android-fragmentactivity,android-databinding,android-json,Android,Android Fragments,Android Fragmentactivity,Android Databinding,Android Json,我正在检索我的JSON,如下所示: myResponse.java: public List<Repo> getData() { return data; } public void setData(List<Repo> data) { this.data = data; } @Override public boolean equals(Object o) { if (thi

我正在检索我的JSON,如下所示: myResponse.java:

  public List<Repo> getData() {
        return data;
    }

    public void setData(List<Repo> data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MyResponse)) return false;

        MyResponse that = (MyResponse) o;

        return data != null ? data.equals(that.data) : that.data == null;

    }

    @Override
    public int hashCode() {
        int result = statusCode.hashCode();
        result = 31 * result + message.hashCode();
        result = 31 * result + (data != null ? data.hashCode() : 0);
        return result;
    }

    public static class Repo {


        @Expose
        @SerializedName("title")
        private String title;



        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Repo)) return false;

            Repo repo = (Repo) o;

            if (!id.equals(repo.id)) return false;
            if (!title.equals(repo.title)) return false;
            return description.equals(repo.description);

        }

        @Override
        public int hashCode() {
            int result = id.hashCode();
            result = 31 * result + title.hashCode();
            return result;
        }
    }
这是我的fragment2新实例:

 public static Fragment2 newInstance() {
        Bundle args = new Bundle();
        Fragment2 fragment = new Fragment2();
        fragment.setArguments(args);
        return fragment;
    }
是否有一种方法可以将从JSON检索到的属性标题和描述从片段1传递到片段2?
我想在fragment2文本视图中显示这些属性,但不确定最好的方法是什么。尝试联机查找,但迄今为止运气不佳。有什么想法吗?

在创建新的
片段时,您希望通过
传入数据

public static Fragment2 newInstance(Repo repo) {

    Fragment2 fragment = new Fragment2();

    Bundle args = new Bundle();
    args.putString("title", repo.getTitle());
    args.putString("description", repo.getDescription());

    fragment.setArguments(args);

    return fragment;
}
然后在
片段2
中,您可以再次获取它们

   void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle arguments = getArguments();

        String title = arguments.getString("title", "");
        String description = arguments.getString("description", "");
    }

@再见,狗,你是有史以来最好的男孩!谢谢你的解决方案。它起作用了!
   void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle arguments = getArguments();

        String title = arguments.getString("title", "");
        String description = arguments.getString("description", "");
    }