Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate A ListView In One Fragment From A Second Fragment
    primarykey
    data
    text
    <p>I have a <code>ViewPager</code> that shows the user 2 fragments.</p> <ul> <li><p>Fragment 1 contains a button with an <code>onClickListener</code> that adds an entry to the <code>ListView</code> contents in fragment 2. The new entry is a number generated by a random number generator.</p></li> <li><p>Fragment 2 contains a <code>ListView</code> with some random numbers in.</p></li> </ul> <p>I cannot find a way to refresh the list in fragment 2 from the button listener in fragment 1. <strong>How do i do this?</strong></p> <p>I have tried:</p> <ol> <li>initializing and setting the list adapter in an <code>onResume</code> in fragment 2.</li> <li>Calling <code>myViewPager.notifyDataSetChanged</code> from fragment 1.</li> </ol> <p>Neither of the above methods have worked. Any ideas?</p> <hr> <p>Edit: Here is my code:</p> <p><strong>layout_main.xml</strong></p> <pre><code>&lt;android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainpager" android:layout_width="match_parent" android:layout_height="match_parent"/&gt; </code></pre> <p><strong>layout_frag_one.xml</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;Button android:id="@+id/main_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Add Number" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>layout_frag_two</strong></p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>MainActivity</strong></p> <pre><code>public class SlideActivity extends FragmentActivity { private final static int FRAGMENT_NUMBER = 2; private ViewPager pager; protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_mainpager); pager = (ViewPager) findViewById(R.id.mainpager); pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); super.onCreate(savedInstanceState); } private class ViewPagerAdapter extends FragmentStatePagerAdapter { public ViewPagerAdapter(FragmentManager fm) { super(fm); } public Fragment getItem(int index) { switch(index){ case 0: return new Fragment1(); case 1: return new Fragment2(); default: return new Fragment1(); } } public int getCount() { return FRAGMENT_NUMBER; } } } </code></pre>
    singulars
    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