Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot refresh/update listview in a fragment from another fragment in ViewPager
    primarykey
    data
    text
    <p>I am having a hard time figuring out the next thing.</p> <p><strong>What I have:</strong> I have a viewpager and several pages in it. In this question only two of them is important, lets call them Fragment1 and Fragment2 and they are next to each other. Fragment1 contains a listview which is filled with data from the internet (external database). Fragment2 contains a simple button.</p> <p><strong>My goal:</strong> If I click on the button in Fragment2, I add a new item to the external database. I would like to update/refresh the listview in the Fragment1 with this new item.</p> <p>The <code>notifyDataChanged()</code> doesnt work in my case, however so far I was convinced that it reinstantiates every pages.. I am going to introduce my problem the clearest way I can, so lets see the code I have, this is my <code>ViewPager</code> adapter:</p> <pre><code>class MyPagerAdapter extends FragmentStatePagerAdapter { public List&lt;String&gt; fragmentsA; public MyPagerAdapter(FragmentManager fm) { super(fm); fragmentsA = fragments; } @Override public Fragment getItem(int position) { return Fragment.instantiate(context, fragmentsA.get(position)); } @Override public CharSequence getPageTitle(int position) { return mEntries.get(position % CONTENT.length).toUpperCase(); } @Override public int getCount() { return mEntries.size(); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } } </code></pre> <p>Fragment1 onCreateView() (shortly):</p> <pre><code>public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { getData(); View view = inflater.inflate(R.layout.latestapps_tab, container, false); lw = (ListView) view.findViewById(R.id.lw); context = getActivity().getApplicationContext(); act = this.getActivity(); m_adapter = new ItemAdapter(); lw.setAdapter(m_adapter); return view; } </code></pre> <p>I create the <code>ViewPager</code> and the adapter, I set the adapter for the <code>ViewPager</code> afterwards I fill the my viewpager with my fragments in my Main class. After this I am goint to have a fully functional ViewPager with 2 fragments. </p> <pre><code> pager = (ViewPager)findViewById( R.id.viewpager ); adapter = new MyPagerAdapter(getSupportFragmentManager()); indicator = (TabPageIndicator)findViewById( R.id.indicator ); pager.setAdapter( adapter ); indicator.setViewPager( pager ); pager.setCurrentItem(INITIAL_PAGE); pager.setOffscreenPageLimit(3); //adding fragments to the pager fragments.add( Fragment1.class.getName()); fragments.add( Fragment2.class.getName()); </code></pre> <p>In the Fragment1 I have a listview with some textviews in every list item. The loading works perfectly: I create the <code>ArrayLists</code> and I fill thoes lists with data from the external database. After loading is done, I fill the listviews with these tons of data. In Fragment 2 I click on the button and I would like that listview to be updated so a new row should be created in the listview with some data from the external database. (of course writing into the database works)</p> <p>My guess, that I might not refresh the ArrayLists or I dont reinstantiate the Fragment1, so the getDataFromSQL() method never turns only if I exit and launch the application again or I swipe so much in the ViewPager that the Fragment1 gets detached. So I cannot update or refresh the Fragment1. Could someone help in this questionL?</p> <p><strong>EDIT</strong></p> <p>I managed to make it happen with delivering a message to the fragment2 to update itself. But I am not sure if it is a good solution and there is not a better way, i.e. just refreshing somehow the whole fragment. </p> <p><strong>SOLUTION</strong></p> <p>Okay I think it must have been my laziness but I solved it now. For everyone who still wants to refresh a fragment from another one or just make conection between fragments, I tell you the appropriate approach:</p> <ol> <li><p>You have to implement your own listener which helps you communicate between the fragments through the holder activity. This can be found here: <a href="http://developer.android.com/training/basics/fragments/communicating.html" rel="nofollow noreferrer">http://developer.android.com/training/basics/fragments/communicating.html</a> . Very simple and useful.</p></li> <li><p>You have to retrieve the fragment, which is again simple: <a href="https://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager">Retrieve a Fragment from a ViewPager</a> These Q offers several acceptable way, I used the <code>SpareArray</code> solution.</p></li> </ol> <p>Thank you for the help anyway!</p>
    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.
 

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