Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In relation to notifyDataSetChanged() function, I have two different experiences. </p> <p>When I used my subclass of <a href="http://developer.android.com/reference/android/widget/ArrayAdapter.html" rel="nofollow">ArrayAdapter</a> to update a ListView, it worked straight. When I called notifyDataSetChanged(), the ListView items are updated.</p> <p>However, when I used a subclass of <a href="http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html#pubmethods" rel="nofollow">FragmentStatePagerAdapter</a> to update a ViewPager, calling notifyDataSetChanged() did not work. NotifyDataSetChanged() is responsible for calling the onChanged() function of all registered <a href="http://developer.android.com/reference/android/database/DataSetObserver.html" rel="nofollow">DataSetObserver</a>, but the observers have not been registered automatically like in ArrayAdapter. You have to do the ground work by yourself. </p> <p>To my surprise, DataSetObserver is not a interface but an abstract class. So I could not simply implement it in my subclass of fragment. I have to define a subclass of DataSetObserver, then define a member field in this class as a reference to my fragment, and in the overriden onChanged() function update your View display. The code is as follows:</p> <pre><code>public class DetailFragDataSetObserver extends DataSetObserver { private DetailTextFragment view_instance; protected static ArrayList&lt;DetailFragDataSetObserver&gt; list = new ArrayList&lt;DetailFragDataSetObserver&gt;(); public DetailFragDataSetObserver(DetailTextFragment f){ view_instance = f; list.add(this); } public DetailTextFragment getView(){ return view_instance; } public static DataSetObserver findDataSetObserverFor(DetailTextFragment f){ for ( int i = 0; i&lt;list.size(); i++){ if ( list.get(i).getView() == f) return list.get(i); } return null; } public static void release(DataSetObserver observer){ list.remove(observer); ((DetailFragDataSetObserver)observer).view_instance = null; } @Override public void onChanged(){ assert null != view_instance; view_instance.vUpdateData(); } } </code></pre> <p>You have to be careful. You need to remember to nullify the reference and unregister the DataSetObserver object in your overriden fragment.onDestroy(), otherwise the fragment cannot be garbage collected.</p> <p>Later, through looking into the source codes, I found ArrayAdapter and FragmentStatePagerAdapter and are not siblings. They are totally different. They are actually from different packages:</p> <pre><code> android.widget.ArrayAdapter android.support.v4.app.FragmentStatePagerAdapter </code></pre> <p>That's why the former worked easily while the latter does not.</p>
    singulars
    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.
    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