title: How to parse JSON data into fragments?
body: <p>Hello friends im developing android app with fragments in a viewpager but now i want to parse JSON data into the fragment.my question is how can i set number of fragments as the size of json data and insert the json data into the required fragment.please help me ive been searching for it on internet for hours but disappointment results so please help me.Thanks in advance!.</p>
  
  <pre><code>[{"c_id":13,"category_name":"PUBLICATIONS","imagename":"http://goringr.com/church_project/churchimagesnew/publications.jpg","ctype":"church"},{"c_id":14,"category_name":"YOUTH ASSOCIATION","imagename":"http://goringr.com/church_project/churchimagesnew/youthassociation.jpg","ctype":"church"}]
  </code></pre>
  
  <p>this is my json data and all i want is to display contents of object with c_id=13 in first fragment and display contents of object with c_id=14 in second fragment.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package project1.test;
  
  import android.content.Intent;
  import android.support.design.widget.CollapsingToolbarLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  
  public class MainActivity extends AppCompatActivity {
  public static final String data = "hello";
  FragmentPagerAdapter adapterViewPager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
     //        Toolbar tool = (Toolbar)findViewById(R.id.tool);
     //        setSupportActionBar(tool);
      //        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
      adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
      vpPager.setAdapter(adapterViewPager);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.sample_actions, menu);
      return true;
  }
  
  public static class MyPagerAdapter extends FragmentPagerAdapter {
      private static int NUM_ITEMS = 3;
  
      public MyPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      // Returns total number of pages
      @Override
      public int getCount() {
          return NUM_ITEMS;
      }
  
      // Returns the fragment to display for that page
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  return FirstFragment.newInstance(0, "Fragment1");
              case 1:
                  return FirstFragment.newInstance(1, "Fragment2");
              case 2:
                  return SecondFragment.newInstance(2, "Fragment3");
              default:
                  return null;
          }
      }
  
      // Returns the page title for the top indicator
      @Override
      public CharSequence getPageTitle(int position) {
          return "Page " + position;
      }
  
  }
  
  } 
  </code></pre>
  
  <p><strong>FirstFragment</strong></p>
  
  <pre><code>public class FirstFragment extends Fragment {
  // Store instance variables
  private String title;
  private int page;
  
  // newInstance constructor for creating fragment with arguments
  public static FirstFragment newInstance(int page, String title) {
      FirstFragment fragmentFirst = new FirstFragment();
      Bundle args = new Bundle();
      args.putInt("someInt", page);
      args.putString("someTitle", title);
      fragmentFirst.setArguments(args);
      return fragmentFirst;
  }
  
  // Store instance variables based on arguments passed
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setHasOptionsMenu(true);
      page = getArguments().getInt("someInt", 0);
      title = getArguments().getString("someTitle");
  }
  
  // Inflate the view for the fragment based on layout XML
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
  ;
      View view = inflater.inflate(R.layout.fragment_first, container, false);
   //        TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
   ////        tvLabel.setText(page + " -- " + title);
  
  
      CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)view.findViewById(R.id.collapsing_toolbar1);
      collapsingToolbarLayout.setTitle("black panther");
  
      return view;
  }
  
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // TODO Add your menu entries here
      super.onCreateOptionsMenu(menu, inflater);
      inflater.inflate(R.menu.sample_actions, menu);
  }
  }
  </code></pre>
  
  <p><strong>SecondFragment</strong></p>
  
  <pre><code>public class SecondFragment extends Fragment {
  // Store instance variables
  private String title;
  private int page;
  Button tryit;
  
  // newInstance constructor for creating fragment with arguments
  public static SecondFragment newInstance(int page, String title) {
      SecondFragment fragmentSecond = new SecondFragment();
      Bundle args = new Bundle();
      args.putInt("someInt", page);
      args.putString("someTitle", title);
      fragmentSecond.setArguments(args);
      return fragmentSecond;
  }
  
  // Store instance variables based on arguments passed
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      page = getArguments().getInt("someInt", 0);
      title = getArguments().getString("someTitle");
  }
  
  // Inflate the view for the fragment based on layout XML
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_second, container, 
     false);
      tryit = (Button) view.findViewById(R.id.tryit);
      tryit.setOnClickListener(new Button.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent intent = new Intent(getActivity(),ManagingProfile.class);
              startActivity(intent);
          }
      });
  //        TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
  //        tvLabel.setText(page + " -- " + title);
      return view;
  }
  }
  </code></pre>