-1

How do i add bundle i created to:

int index = 0;

Here is the code i am trying to add bundle. Where am i getting wrong?

public class NewsItemFragment extends Fragment {
public static final String KEY_NEWS_ITEM_INDEX = "news_item_index";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO:  Hmmm, how do we get this index?
    int index = 0;
    getActivity().setTitle(News.headlines[index]);
    View view = inflater.inflate(R.layout.fragment_newsitem, container, false);
    return view;
}

    public static NewsItemFragment createInstance(int index) {
    NewsItemFragment fragment = new NewsItemFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("news_item_index", index);
    fragment.setArguments(bundle);
    index = bundle.getArguments().getInt( NewsItemFragment.KEY_NEWS_ITEM_INDEX);
    return fragment;
}


@Override
public void onStop() {
    super.onStop();
    getActivity().setTitle(getResources().getString(R.string.app_name));
}

}

here is the link to this question:

https://teamtreehouse.com/community/great-job-thanks-now-in-newsitemfragmentjava-update-the-line-that-says-int-index-0-to-access-the-new-bundle

ankuranurag2
  • 2,300
  • 15
  • 30

2 Answers2

1

just replace int index = 0; with

int index = getArguments().getInt( NewsItemFragment.KEY_NEWS_ITEM_INDEX);

Alim Parkar
  • 632
  • 3
  • 9
0

You have here an example:

  • Fragment that sends information

frag_more_informations frag_more_informations = new frag_more_informations(); Bundle bundle = new Bundle(); bundle.putCharSequence(bundle_string, txt_results_experience.getText()); frag_more_informations.setArguments(bundle); FragmentManager manager = getActivity().getSupportFragmentManager(); manager.beginTransaction().replace(R.id.framelayout_secundary, frag_more_informations, frag_more_informations.getTag()).commit();

  • Fragment that receives it

Bundle bundle = this.getArguments(); if (bundle != null) { CharSequence myInt = bundle.getCharSequence(bundle_string); txt_results_experience.setText(String.valueOf(myInt)); }

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26