java - Dynamically updating a fragment -


i have mainactivity has multiple tabs. each tab showlistfragment , extends fragment. now, each tab contains data fetch database. have move-button moves data 1 tab in each fragment:

@override public void onclick(dialoginterface dialog, int listindex) {     database.add(listindex,object);     database.remove(listindex,object); } 

the fragment not update directly, after few swipes in between tabs (3 exactly). how force fragment update instantaneous after clicked button? don't want manage through onpageselected in viewpager, since not update fragment i'm on, after i've swiped next fragment. , don't want update data after each swipe.

i know maybe need use kind of observer pattern this: how make class extend observable when has extended class too? still, i'm still not sure how update fragment directly, , how apply observer/event pattern in application.

since fragments can access activity enough getactivity(), make activity central hub dispatching updates.

it sounds have persistence part handled database , need update events. here goes:

  1. define listener interface. inner interface within activity:

        public interface dataupdatelistener {         void ondataupdate();     } 
  2. add data structure activity keep track of listeners:

        private list<dataupdatelistener> mlisteners; 

    don't forget initialize in constructor:

            mlisteners = new arraylist<>(); 
  3. add register/unregister methods activity:

        public synchronized void registerdataupdatelistener(dataupdatelistener listener) {         mlisteners.add(listener);     }      public synchronized void unregisterdataupdatelistener(dataupdatelistener listener) {         mlisteners.remove(listener);     } 
  4. add event method activity:

        public synchronized void dataupdated() {         (dataupdatelistener listener : mlisteners) {             listener.ondataupdate();         }     } 
  5. have fragments implement dataupdatelistener:

    public class myfragment extends fragment implements dataupdatelistener { 

    and implement method

        @override     public void ondataupdate() {         // put ui update logic here     } 
  6. override onattach() , ondestroy() in fragments register/unregister:

        @override     public void onattach(activity activity) {         super.onattach(activity);         ((mainactivity) activity).registerdataupdatelistener(this);     }      @override     public void ondestroy() {         super.ondestroy();         ((mainactivity) getactivity()).unregisterdataupdatelistener(this);     } 
  7. fire event in fragment's ui update event:

        @override     public void onclick(dialoginterface dialog, int listindex) {         database.add(listindex,object);         database.remove(listindex,object);         ((mainactivity) getactivity()).dataupdated();     } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -