-1

I am retrieving my JSON as follows : 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;
        }
    }

Now in my fragment 1 adapter I have :

 itemView.setOnClickListener(v -> {
                AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
                activity.getSupportFragmentManager()
                        .beginTransaction()
                        .disallowAddToBackStack()
                        .setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
                        .add(R.id.root_view, Fragment2.newInstance(), Fragment2.TAG)
                        .commit();

This is my fragment2 new instance:

 public static Fragment2 newInstance() {
        Bundle args = new Bundle();
        Fragment2 fragment = new Fragment2();
        fragment.setArguments(args);
        return fragment;
    }

Is there a way I can pass the attributes title and description I retrieve from JSON from fragment 1 to fragment 2 ? I would like to display these attributes in the fragment2 textview, but not sure what will be the best approach. Tried looking online but had no luck so far.Any ideas?

1 Answers1

1

When creating your new Fragment, you want to pass in your data through the Bundle.

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;
}

And then within your Fragment2, you can fetch them again.

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

        Bundle arguments = getArguments();

        String title = arguments.getString("title", "");
        String description = arguments.getString("description", "");
    }
advice
  • 5,778
  • 10
  • 33
  • 60
  • @Adice-Dog you are the best boy ever! Thanks for your solution. it works! –  Mar 23 '19 at 11:54