Note that there are some explanatory texts on larger screens.

plurals
  1. POPass parameters between viewpager fragments
    primarykey
    data
    text
    <p>I have an activity that holds 2 fragments, one for list and one for detail. What I would like to do is, whenever a list item is clicked the related parameters will be sent to detail fragment. But I couldn't achieve it.</p> <p>Here is activity:</p> <pre><code>public class ActivityMain extends ActionBarActivity{ /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ List&lt;String&gt; naviList = new ArrayList&lt;String&gt;(); ViewPager mViewPager; private ActionBarDrawerToggle drawerToggle; private DrawerLayout drawer; ListView navList; DrawerAdapter naviAdapter; private static final int GRAVITY = Gravity.LEFT; private static final String jsonURL = ""; List&lt;String&gt; categories = new ArrayList&lt;String&gt;(); int check = -1, listCheck = 0; Dialog d; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); //some methods (e.g. navi-drawer etc.) } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. drawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); drawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.medicalendar_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { case R.id.action_settings: return true; } if (drawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return ListFragment.newInstance("FirstFragment, Default"); case 1: return DetailFragment.newInstance("DetailFragment, Detail"); default: return ListFragment.newInstance("FirstFragment, Default"); } } @Override public int getCount() { return 2; } } private boolean version() { if (Build.VERSION.SDK_INT &gt; 11) { return true; } else { return false; } } </code></pre> <p>My List Fragment:</p> <pre><code>public class ListFragment extends Fragment { ListView list; LazyAdapter adapter; List&lt;String&gt; naviList = new ArrayList&lt;String&gt;(); RelativeLayout loading; EventsParser parser; List&lt;Event&gt; events = new ArrayList&lt;Event&gt;(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_list, container, false); parser = new EventsParser(""); events = parser.getITEMS(); list = (ListView) v.findViewById(R.id.list); adapter = new LazyAdapter(getActivity(), events); list.setAdapter(adapter); return v; } public static ListFragment newInstance(String text) { ListFragment f = new ListFragment(); Bundle b = new Bundle(); b.putString("msg", text); f.setArguments(b); return f; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { list.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { ViewPager vp = (ViewPager) getActivity().findViewById(R.id.pager); //clicked item's data to pass next page. vp.setCurrentItem(1); } }); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } </code></pre> <p>My Detail Fragment:</p> <pre><code>public class DetailFragment extends Fragment { View v; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.fragment_detail, container, false); ImageButton imageButton = (ImageButton) v.findViewById(R.id.d_map); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity().getApplicationContext(), "Navigating...", Toast.LENGTH_LONG).show(); } }); return v; } public static DetailFragment newInstance(String text) { DetailFragment f = new DetailFragment(); Bundle b = new Bundle(); b.putString("msg", text); f.setArguments(b); return f; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { } </code></pre> <p>If you could help me I would be greatly appriciated.</p> <p>Best,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload